PHP Advanced Concepts

Quote Tricks

Suppose you want to include A LOT of text (like several lines of code) as a String variable, and you don't want to escape all the possible quotation problems. Try this trick (called the Heredoc Syntax):

<?php $content = <<<END_CONTENT <select name="select1"> <option value="abc">ABC</option> <option value="def">DEF</option> <option value="ghi">GHI</option> <option value="jkl">JKL</option> <option value="mno">MNO</option> <option value="pqr">PQR</option> <option value="stu">STU</option> </select> END_CONTENT; ?>

Now, if you try to echo the variable, you get:

Heredoc text behaves just like a double-quoted string, without the double-quotes. This means that you do not need to escape quotes in your here docs, but you can still use escape codes as elsewhere.

The end identifier (END_CONTENT; in the above example) must be the only thing on the line (it must be preceeded and followed by a '\r' and it must begin at the first character of the line.

Processing Speed

Scripts using single quotes run slightly faster because the PHP parser can include the string directly. Double quoted strings are slower because they need to be parsed.

Anatomy of a Class   < <  PREVIOUS   Table of Contents NEXT  > >   Construct Tricks

Developed with HTML-Kit
Sandersongs Web Tutorials
Contact the Webmasterwith comments.
©2018, by Bill Sanders, all rights reserved.
This domain had 3,230 different visits in the last 30 days.
464,206 hits on this domain since 24 Nov 2006.
This page was last modified on our server on 4 Jun 2008
and last refreshed on our server at 10:36 pm, MST
This file took 0.0099 seconds to process.