PHP脚本标记

Why does this if statement have each of its conditionals wrapped in PHP tags?

  <?php if(!is_null($sel_subject)) { //subject selected? ?>
     <h2><?php echo $sel_subject["menu_name"]; ?></h2>
  <?php } elseif (!is_null($sel_page)) { //page selected? ?>
     <h2><?php echo $sel_page["menu_name"]; ?></h2>
  <?php  } else { // nothing selected ?>
     <h2>Select a subject or a page to edit</h2>
  <?php } ?>  

Because there is html used. Jumping between PHP and HTML is called escaping.

But I recommend you not to use PHP and HTML like this. May have a look to some template-systems e.g. Smarty or Frameworks with build-in template-systems like e.g. Symfony using twig.

Sometimes its ok if you have a file with much HTML and need to pass a PHP variable.

Sample

<?php $title="sample"; ?>
<html>
    <title><?php echo $title; ?></title>
    <body>
    </body>
</html>

This is not much html but a sample how it could look like.

That sample you provided us should more look like....

<?php 
    if(!is_null($sel_subject)) 
    {   //subject selected?
        $content = $sel_subject["menu_name"];
    } 
    else if (!is_null($sel_page)) 
    {   //page selected?
        $content = $sel_page["menu_name"];
    } 
    else 
    {   // nothing selected
        $content = "Select a subject or a page to edit";
    }
    echo "<h2>{$content}</h2>";
?> 

You could echo each line of course. I prefer to store this in a variable so I can easy prevent the output by editing one line in the end and not each line where I have added a echo.

According to some comments i did a approvement to the source :)

Because the <h2> tags are not PHP and will display an error if the PHP Tags are removed.
This code will display one line of text wrapped in <h2> tags.

This is called escaping.

Because you cannot just type html between your php tags.

However, I would rather use the following syntax because it is easier to read. But that depends on the programmers opinion.

<?php 
    if(!is_null($sel_subject)) 
    { //subject selected?
        echo "<h2>" . $sel_subject["menu_name"] . "</h2>";
    } 
    elseif (!is_null($sel_page)) 
    { //page selected? 
        ehco "<h2>" . $sel_page["menu_name"] . "</h2>";
    } 
    else 
    { // nothing selected
        echo "<h2>Select a subject or a page to edit</h2>";
    } 

Because inside the if-statement there is an HTML code, which you can put it by closing PHP tags and open it again like this:

<?php if(/*condition*/){ ?> <html></html> <?php } ?>

or:

<?php if(/*condition*/){  echo '<html></html>' ; }

That is because in this snippet we see html and php code. The code <?php changes from html-mode to php-mode and the code ?> changes back to html-mode.

There are several possibilites to rewrite this code to make it more readable. I'd suggest the following:

<?php
    //subject selected?
    if (!is_null($sel_subject)) {
        echo "<h2>" . $sel_subject["menu_name"] . "</h2>";

    //page selected?
    } elseif (!is_null($sel_page)) {
        echo "<h2>" . $sel_page["menu_name"] . "</h2>";

    // nothing selected
    } else {
        echo "<h2>Select a subject or a page to edit</h2>";
    }
?>

using the echo-command to output html, you don't need to change from php-mode to html-mode and you can reduce the php-tag down to only one.