HTML and CSS Reference
In-Depth Information
Take a look at the last string in the list. The opening quotation mark is on the first line,
and the closing quotation mark is on the second line. In PHP, this is completely valid. In
some programming languages, strings that span multiple lines are illegal—not so in PHP,
where strings can span as many lines as you like, so long as you don't accidentally close
the quotation marks.
There's more to strings than just defining them. You can use the . operator to join
strings, like this:
$html_paragraph = “<p>” . $paragraph . “</p>”;
The $html_paragraph variable will contain the contents of $paragraph surrounded by
the opening and closing paragraph tag. The . operator is generally referred to as the
string concatenation operator .
Up to this point, you might have noticed that sometimes I've enclosed strings in double
quotation marks, and that other times I've used single quotation marks. They both work
for defining strings, but there's a difference between the two. When you use double quo-
tation marks, PHP scans the contents of the string for variable substitutions and for spe-
cial characters. When you use single quotation marks, PHP just uses whatever is in the
string without checking to see whether it needs to process the contents.
Special characters are introduced with a backslash, and they are a substitute for charac-
ters that might otherwise be hard to include in a string. For example, \n is the substitute
for a newline, and \r is the substitute for a carriage return. If you want to include a new-
line 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>';
21
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>”;
 
Search WWH ::




Custom Search