Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

Mixed-Mode Processing

A key concept in PHP is that you can toggle PHP parsing mode whenever and as often as you want, even inside a code block. Here is a basic PHP script:

    <?php
            if ($logged_in =  = true) {
                    print "Lots of stuff here";
                    print "Lots of stuff here";
                    print "Lots of stuff here";
                    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 $logged_in is true. 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 ($logged_in =  = true) {
    ?>
            Lots of stuff here
            Lots of stuff here
            Lots of stuff here
            Lots of stuff here
            Lots of stuff here
    <?php
            }
    ?>

The Lots of stuff here lines are still only sent to output if $logged_in is true, but we exit PHP mode to print it out. We then reenter PHP mode to close the if statement and continue—it makes the whole script easier to read.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page