A key concept in PHP is that you can toggle PHP parsing mode whenever you want and as often as you want, and you can even do it inside a code block. Here is a basic PHP script:
<?php
if ($foo == $bar) {
print "Lots of stuff here";
print "Lots of stuff here";
print "Lots of stuff here";
...[snip]...
print "Lots of stuff here";
print "Lots of stuff here";
}
?>
As you can see, there are a lot of print statements that will only be executed if the variable $foo is the same as variable $bar. All the output is encapsulated into print statements, but PHP allows you to exit the PHP code island while still keeping the if statement code block open - here's how that looks.
<?php
if ($foo == $bar) {
?>
Lots of stuff here
Lots of stuff here
Lots of stuff here
...[snip]...
Lots of stuff here
Lots of stuff here
<?php
}
?>
The "Lots of stuff here" lines are still only sent to output if $foo is equal to $bar, but we exit PHP mode to print it out. We then re-enter PHP mode to close the if statement, and continue - it makes the whole script easier to read.
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Including other files >>
Previous chapter: Loops within loops
Jump to:
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.