It is necessary to show the HTML code under a certain condition. Is it right to write?

<?php if ($time == 1) { ?> <div class="coming_soon"> <div class="icon"></div> <p>Здесь скоро появится новый урок...</p> </div> <?php }; ?> 

Everything works, but for some reason I doubt it.

  • 6
    can. just don't use curly braces in patterns. there are special abbreviations for this (I don’t remember how correctly they are called) <?php if ($time == 1) : ?> and close as <?php endif; ?> <?php endif; ?> or <?php foreach ($time as $item) : ?> and close like <?php endforeach; ?> <?php endforeach; ?> and so on ... so that you can see the correspondence between each other ............ php.net/manual/ru/control-structures.alternative-syntax.php - Alexey Shimansky
  • one
    @ Alexey Shimansky use braces. We get rid of all endif, it's some kind of basic. - Goncharov Alexander
  • 6
    @GoncharovAleksandr this syntax is almost specifically created just for the template. Using brackets in the template is also stupid, because they are difficult to keep track of at break, especially when there may be several nestings separated quite far. In the bare code, a la controller, for example, this syntax is absolutely not necessary and idiotic, yes. But in templates it is just the opposite. So you are wrong. And categorically - Alexey Shimansky
  • 2
    @ Alexey Shimansky I can’t be wrong, because it is in taste and color, that is, it is an opinion. We all agreed that curly braces are much more elegant and shorter, including patterns. And т.к. их сложно уследить при разрыве т.к. их сложно уследить при разрыве - yes no, any IDE will highlight it for a long time, click after the curly bracket, you will see the end. Yes, and with whom I talked - nobody wants to see the BASIC; in general, for the first time, I see someone who likes endif; :) - Goncharov Alexander
  • four
    @GoncharovAleksandr я не могу быть не правым - т.к. это на вкус и цвет я не могу быть не правым - т.к. это на вкус и цвет .... it’s a contradiction, so for a second ;-) ...... you can negotiate anything within the company, at least about inserting unicode raccoon drawings between the lines. This does not make it common practice. If you look at the queries of the search engine "alternative php syntax" you will not see in the responses to the request of braces, because the common practice is different. Including php.net shows exactly this practice (via endforeach, etc.) ... in general, I expressed myself - Alexey Shimansky

2 answers 2

It all depends on your programming style and patterns used. There are many ways to display HTML. It is best to have a single style in your application.

I prefer to output HTML like this:

 echo <<<HTML div class="coming_soon"> <div class="icon"></div> <p>Здесь скоро появится новый урок...</p> </div> HTML; 

This helps when navigating through files. It is enough to include search in <<<HTML .

But if you use the MVC pattern, then the logic should be separate, and the views separately. Since often the coder does not know PHP and vice versa.

Part of the PHP file controller:

 <?php if ($time == 1) { return include $this->viewsPath.'_comming_soon.php'; } 

View _comming_soon.php :

 <div class="coming_soon"> <div class="icon"></div> <p>Здесь скоро появится новый урок...</p> </div> 
  • for every sneeze to create a view?))) Which will be needed one unfortunate time ??? Oooh - Sergey V.
  • @ SergeyV. Your right. But if you don’t, when you’re bloating PHP, it’s not developed to the level of ++, java or Sharp. - Makarenko_I_V Nov.

All right Here is another example:

 <?php if ($time == 1): ?> <div class="coming_soon"> <div class="icon"></div> <p>Здесь скоро появится новый урок...</p> </div> <?php endif; ?>