Prev | Current Page 169 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

\n
You might assume that $animal is a variable and that \n is a newline character, and
therefore both should be interpreted accordingly. However, what if you want to output
the string exactly as it is written, or perhaps you want the newline to be rendered but
want the variable to display in its literal form ($animal), or vice versa? All of these variations
are possible in PHP, depending on how the strings are enclosed and whether
certain key characters are escaped through a predefined sequence. These topics are
the focus of this section.
Double Quotes
Strings enclosed in double quotes are the most commonly used in most PHP scripts
because they offer the most flexibility. This is because both variables and escape
sequences will be parsed accordingly. Consider the following example:
$sport = "boxing";
echo "Jason's favorite sport is $sport.";
?>
This example returns the following:
Jason's favorite sport is boxing.
Escape sequences are also parsed. Consider this example:
$output = "This is one line.\nAnd this is another line.";
echo $output;
?>
96 CHAPTER 3 ?–  PHP B ASICS
This returns the following within the browser source:
This is one line.
And this is another line.
It??™s worth reiterating that this output is found in the browser source rather than in
the browser window. Newline characters of this fashion are ignored by the browser
window.


Pages:
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181