ptg16476052
660 LESSON 24: Taking Advantage of the Server
newline, and \r is the substitute for a carriage return. If you want to include a newline in
a string and keep it all on one line, just write it like this:
$multiline_string = "Line one\nLine two";
Here’s what I mean by variable substitutions. In a double-quoted string, I can include a
reference to a variable inside the string, and PHP will replace it with the contents of the
variable when the string is printed, assigned to another variable, or otherwise used. In
other words, I could have written the preceding string-joining example as follows:
$html_paragraph = "<p>$paragraph</p>";
PHP will find the reference to $paragraph within the string and substitute its contents.
On the other hand, the literal value “$paragraph” would be included in the string if I
wrote that line like this:
$html_paragraph = '<p>$paragraph</p>';
You need to do a bit of extra work to include array values in a string. For example, this
won’t work:
$html_paragraph = "<p>$paragraph['intro']</p>";
You can include the array value using string concatenation:
$html_paragraph = "<p>". $paragraph['intro']. "</p>";
You can also use array references within strings if you enclose them within curly braces,
like this:
$html_paragraph = "<p>{$paragraph['intro']}</p>";
One final note on defining strings is escaping. As you know, quotation marks are com-
monly used in HTML as well as in PHP, especially when it comes to defining attributes
in tags. There are two ways to use quotation marks within strings in PHP. The first is
to use the opposite quotation marks to define the string that you’re using within another
string. Here’s an example:
$tag = '<p class="important">';
I can use the double quotes within the string because I defined it using single quotes. This
particular definition won’t work, though, if I want to specify the class using a variable. If
that’s the case, I have two other options:
$tag = "<p class=\"$class\">";
$tag = '<p class="'. $class. '">';
In the first option, I use the backslash character to “escape” the double quotes that occur
within the string. The backslash indicates that the character that follows is part of the