<?php
if ($expression) {
?>
<strong>This is true.</strong>
<?php
} else {
?>
<strong>This is false.</strong>
<?php
}
?>
I am confused by this code.
Can anyone explain to me what happens if $expression is true? false?
Name:
Anonymous2008-06-03 20:00
This was an example of advanced escaping (according to php).
It's just hard to wrap my head around what exactly the php-engine is doing. For me, there's two ways of looking at this:
(1) PHP reads through the entire file, reassembles the fragmented code, and then interprets it. So the example would reassembled into <?php if ($expression) {} else {} ?>. Afterwards, anything between the appropriate curly braces would be outputted.
(2) PHP reads and evaluates in parts. It reads/evaluates if($expression) and if it's true it then tries to go into the block { but hits the closing tag ?> and pauses, outputting everything until the opening tag <?php and ending with }. But it must read the else statement and the { and then pause again for the closing tag ?>. Normally it would output anything beyond this tag, but it doesn't. Why? How does it know to prevent the HTML code from being output in this case. If this were C, it would be a matter of whether to jump to a block of code or not. But with broken code, it can't know what the block is unless it's read through the entire file as in (1). This is why here we assume it reads through everything sequentially and evaluates only what's necessary. Is that even possible to do?
I guess it stores the HTML code in order. Which means there is another option.
(3) It must make an entire php file out of the combination as follows
if ($expression) { echo "whatever was here in the file" }
else { echo "whatever was here in the file" }
Thats all I can think of.