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.
©2024, by Bill Sanders, all rights reserved.
This domain had 2,996 different visits in the last 30 days.
This page was last modified on our server on 11 Jul 2021
and last refreshed on our server at 7:28 pm, UTC
This file took 0.01021 seconds to process.