-1

Can someone explain to me why this works?

<?php if ($selected_subject_id) { ?>
    <h2>Manage Subject</h2>
<?php } elseif ($selected_page_id) { ?>
    <h2>Manage Page</h2>
<?php } else { ?>
    Please select a subject or a page.
<?php }?>

Basically, if you click a subject "Manage Subject" displays as title if you click a page "Manage Page" displays as title if neither are clicked it displays "Please select a subject or page" ONLY ONE OF THESE ARE DISPLAYED AT A TIME

My question is, why do the titles/msg behave as if they were inside the PHP script (obeying the conditionals) when they are actually outside of it?

Is it because they're "within" the opening and closing braces of the conditional?

In what other situations will this work?

Thanks.

miken32
  • 42,008
  • 16
  • 111
  • 154
Raul Marquez
  • 948
  • 1
  • 17
  • 27
  • *"Is it because they're "within" the opening and closing braces of the conditional?"* ~ yes. See also http://php.net/manual/control-structures.alternative-syntax.php – Phil Oct 01 '14 at 04:51
  • The simplest way to understand it is to treat everything "outside" the `` script as if it's a big `echo` statement with the text as a literal string. But nothing is interpreted within it: there's no variable expansion, no escape sequences, etc. – Barmar Oct 01 '14 at 04:54
  • read this:http://programmers.stackexchange.com/questions/180501/which-is-better-to-include-html-inside-php-code-or-outside-it – Suchit kumar Oct 01 '14 at 04:58

1 Answers1

0

PHP is indeed able to conditionally echo HTML in this manner. This is actually used quite commonly.

You can also echo HTML in a loop.

Scimonster
  • 32,893
  • 9
  • 77
  • 89