More on Entering Strings


Previously, we alluded to the fact that double-quoted and heredoc strings in PHP can contain variable references in them (using $), and that the language engine would know what to do with them.

Variable Expansion in PHP is a powerful feature that allows for the quick and easy mixing of content and programming. There are two ways to use this feature: simple and complex. The former is for straightforward variable usage, array values, or object properties, while the latter allows for more precise expansions (although they are not that much more complicated). Examples of simple expansion are as follows:

 <?php   $type = "simple";   echo "This is an example of a '$type' expansion.";   $type = array("simple", "complex");   echo<<<THE_END        This is also an example of a '$type[0]' array expansion. THE_END; ?> 

When the PHP processor sees a $ in a double-quoted or heredoc string, it proceeds to gobble up as many characters as it can until it can no longer form a valid variable, array index, or object property name. It then evaluates the result and places the value in the resulting string.

This can cause some slight problems if you want to format the output as follows:

 <?php   $content = "cookie";   echo <<<LUNCH   <p align='center'>     The jar is full of $contents.   </p> LUNCH; ?> 

In this case, we wanted to take a singular value and make it plural on output. However, the previous code often prints out

 Notice: Undefined variable: contents in /home/httpd/www/php/ variable_expansion.php on line 9 The jar is full of ; 

Because the PHP language engine starts processing all the characters it can when it first sees the $ character, it takes everything up to the period after contents. However, this is not a valid identifier. In older versions of PHP, you would not be told this, and the variable would have just evaluated to the empty string (""); however, with PHP 5, you get the warning.

To get around this problem, you can use the complex type of variable expansion. The name is a bit misleadingit is not difficult to use, but it supports more controlled variable expansions. To use it, wrap what you want expanded with curly braces. Since there are no escape sequences for curly braces (more properly called brackets) in code, the PHP processor looks for a { character followed immediately by a $ to indicate some sort of variable expansion. Otherwise, it just prints out the bracket and whatever follows.

This allows us do some more interesting expansions:

 <?php   $hour = 16;   $kilometres = 4;   $content = "cookie";   echo "   4pm in 24 hour time is {$hour}00 hours.<br/>\n";   echo <<<DONE    There are {$kilometres}000m in {$kilometres}km.<br/>    The jar is now, indeed, full of ${content}s.<br/> DONE; ?> 

This gives us the output

 4pm in European/military time is 1600 hours. There are 4000m in 4km. The jar is now, indeed, full of cookies.  

If you ever wanted to have the actual character sequence {$ in your output, you would need to escape it as {\$.




Core Web Application Development With PHP And MYSQL
Core Web Application Development with PHP and MySQL
ISBN: 0131867164
EAN: 2147483647
Year: 2005
Pages: 255

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net