Hudzilla.org - the homepage of Paul Hudson
Contents > Introducing PHP > How PHP is written Wish List | Report Bug | About Me ]

2.6.14     Mixed-mode processing

This is NOT the latest copy of this book; click here for the latest version.

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.





<< 2.6.13 Loops within loops   2.6.15 Including other files >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
The same PHP Fan - 06 Sep 2008

Oh yes and I almost forgot to add (for those familiar with TADS):

myFace.ioCrackIn(Me, smile)

The same PHP Fan - 06 Sep 2008

Oh yes and I almost forgot to add (for those familiar with TADS):

myFace.ioCrackIn(Me, smile)

A PHP Fan - 06 Sep 2008

I found this book today (yesterday actually, but I haven't slept yet) and have spent most of the evening and night reading it. I just want to say that after reading everything up to this point I had to pause to jump up and down while shouting and smiling. Php (and this book especially) seems to be going to my head. Provided I don't have a cerebral meltdown I'll most certainly learn (and try to use) as much php I can.

I just wanted to mention it.

Hm... I wonder if php can be spoken (and understood) on the streets somewhere..?

The same PHP User - 06 Sep 2008

The comment-parser stripped out my spaces and tabs, why the two code-examples look the same, which they shouldnt. The top one had Lots-lines beginning with lots of spaces to make it nice-looking, while the bottom one had the same amount of beginning row-spaces as the 2nd example in the original article had...

A PHP User - 06 Sep 2008

Maybe it is considered good form not to do so, but I miss you showing the beginners that strings are not constrained to one line, even without using heredoc.

For example, the above code could be written as:

<?php
if ($foo == $bar) {
print "Lots of stuff here
Lots of stuff here
Lots of stuff here
Lots of stuff here
Lots of stuff here
Lots of stuff here
Lots of stuff here
Lots of stuff here
Lots of stuff here";
}
?>

Or, more accurate, but less viewable:

<?php
if ($foo == $bar) {
print "Lots of stuff here
Lots of stuff here
Lots of stuff here
Lots of stuff here
Lots of stuff here
Lots of stuff here
Lots of stuff here
Lots of stuff here
Lots of stuff here";
}
?>

Just remember to escape (or use heredoc).



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is four plus four?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow