PHP Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 3.00

PHP Script Processing Rules

This section describes the PHP script source code processing rules - all non-PHP text segments are converted into print statements and merged with PHP code segments to form a single code block.

As I mentioned in the previous section, a PHP script source code is a sequence non-PHP text segments and PHP code segments. The PHP engine will process the source code in 3 logical steps:

  • Converting each non-PHP text segment into a PHP code segment with a print statement.
  • Joining all PHP code segments into a single final PHP code block.
  • Executing the final PHP code block.

In anothe word, a PHP script source code can be viewed as a single PHP code block. All non-PHP text segments are just PHP print statements written in a special way.

PHP's processing rules are very similar to Active Server Page (ASP)'s processing rules.

Now let's see an example PHP script source code file:

<?php /* HeadOrTail.php
* Copyright (c) 2003 by Dr. Herong Yang, http://www.herongyang.com/
*/ ?>
I am tossing a coin now. Guess what the result will be? 
<?php $coin = rand(0,1); ?>
<?php if ($coin==1) { ?>
   Head
<?php } else { ?>
   Tail
<?php } ?>

If you process this script with the PHP engine, you will get:

I am tossing a coin now. Guess what the result will be?
   Tail

Based on the PHP engine processing rules, the above script is identical to this script from the PHP engine process point of view:

<?php /* HeadOrTailModified.php
* Copyright (c) 2003 by Dr. Herong Yang. http://www.herongyang.com/
*/ 
print "I am tossing a coin now. Guess what the result will be?\n";
$coin = rand(0,1);  
if ($coin==1) { 
print "   Head\n";
} else { 
print "   Tail\n";
} ?>

Last update: 2005.

Sections in This Chapter

PHP Script Source Code File Format

PHP Script Processing Rules

PHP Statement Delimiter and Comments

Dr. Herong Yang, updated in 2009
PHP Script Processing Rules