In theory, the html block should fall under the if condition and be executed - if true. In this case, false, but html returns.

<?php $shop = "open"; if ($shop == "close") ?> <h3>Этот html-код попадает в блок выполнения php - кода (if true)</h3> <?php echo "Покупаю хлеб"; echo "Иду домой"; ?> 
  • And where is the further if condition? - Eugen Eray
  • braces add - Eugene Tupikov
  • And why do you think that the output of HTML in this case somehow depends on the PHT-code? - PinkTux
  • one
    Great question, by the way :) I did not immediately get it - Ipatiev
  • @PinkTux so he asks - why does not depend - Ipatiev

3 answers 3

You need to write like this:

 <?php $shop = "open"; if ($shop == "close"): ?> <h3>Этот html-код попадает в блок выполнения php - кода (if true)</h3> <?php endif; echo "Покупаю хлеб"; echo "Иду домой"; ?> 

this is an alternate syntax for control structures

or so:

 <?php $shop = "open"; if ($shop == "close") { ?> <h3>Этот html-код попадает в блок выполнения php - кода (if true)</h3> <?php } echo "Покупаю хлеб"; echo "Иду домой"; ?> 

according to the usual standard for setting brackets in the control structure.

strictly speaking, this post does not explain why it does not work. - @ Ipatiev

Sobsn, I missed this moment. But it is already possible to look at the neighboring answers of the participants @ Ipatev and @vp_arth

I do not think they will remove them))

  • Here, I say, braces are necessary - Evgeny Tupikov
  • @ EvgenyTupikov with alternative syntax is not necessary - Alexey Shimansky
  • Shine! It was interesting why without curly brackets does not work as it should! Thank you - Alexander Zharichenko
  • one
    @Alexander Zharichenko strictly speaking, this post does not explain why it does not work. - Ipatiev
  • @ Ipatiev because it is necessary to write control structures together with html? :) - Alexey Shimansky

I think we still have to explicitly write why the line is displayed.

Without defining the scope, the PHP statements act only on the next statement. I.e,

 if (false) echo 1; echo 2; 

Of course, this code will only output 2.
If we need more operators to be in scope, it should be explicitly defined.

 if (false) { echo 1; echo 2; } 

This code will not output anything.

But the question remains, why PHP did not perceive the HTML output operator, which, it seems, goes the very first after the condition. The answer to this question was given by vp_arth - because the closing PHP tag acts the same as an operator limiter. That is, as a result, the author obtained a condition acting on an empty operator:

 if ($shop == "close") ; # ^ оператор видишь? И я нет. А он есть! 

and further code is not under its effect.

    The construct ?> Assumes an automatic substitution of a semicolon .
    Your code is similar to the following:

     <?php $shop = "open"; if ($shop == "close") ; // <- Условие распространяется только на этот пустой оператор echo '<h3>Этот html-код попадает в блок выполнения php - кода (if true)</h3>'; echo "Покупаю хлеб"; echo "Иду домой"; 

    You should explicitly define the scope of the if statement:

    • @ Alexey Shimansky is a joke? - Ipatiev
    • 3v4l.org/vIeja - vp_arth
    • ))))))))))))))) - Ipatyev
    • @ Ipatiev nea. swore phpfiddle :( .... here it is phpfiddle.org is not good - Alexey Shimansky
    • He even swore simply for the absence of a closing tag at the very end (it is not clear why, really) - vp_arth