HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [199]
$output = “World!”;
print “
Hello “ . $output . “
”;print “
” . $output + 5 . “
”;?>
Figure 1-3: The difference between addition and concatena-tion.
The previous code takes the variable output with the value World and concatenates it to Hello when printed. Next, it adds the variable output to the number 5. When PHP sees the plus sign, it interprets the values on either side of it as numbers. Because output has no logical numerical value, PHP assigns it the value of 0, which it adds to 5, resulting in the output of
5
being sent to the browser.
Interpolating variables into text
If you have a bunch of text to print with variables thrown in, it can get a little tedious to use concatenation to add in the variables. Luckily, you don’t have to!
With PHP, you can include the variables as follows (see Figure 1-4 for the output):
PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
$firstName = ”John”;
$lastName = ”Doe”;
print ”
Hello $firstName $lastName!
”;?>
Figure 1-4: The variables print without having to do concatena-tions.
This process is called interpolation. Because all PHP variables begin with quotes, you can freely put variables right inside your string values, and when PHP sees a variable, it will automatically replace that variable with its value.
Interpolation works only with double-quoted strings because double quotes indicate PHP should process the string before passing it to the user.
Building XHTML Output
The output of a PHP program is usually an XHTML page. As far as PHP is concerned, XHTML is just string data, so your PHP program often has to do a lot of string manipulation. You’ll often be writing long chunks of text (XHTML code) with several variables (generated by your PHP program) interspersed throughout the code. This type of text (XHTML output) will often stretch over several lines, requires carriage returns to be preserved, and often contains special characters like quotes and <> symbols. The ordinary quote symbols are a little tedious if you want to use them to build a Web page. Here’s an example.
Say you wanted to create a program which could take the value of the $name and $address variables and put them into a table like this:
| name | John |
| address | 123 Main St. |
There are a few ways to combine the PHP and XHTML code as shown in the following sections.
Using double quote interpolation
Using regular double quotes, the code would look something like this:
$name = “John”;
$address = “123 Main St.”;
$output = “”;
$output .= “
| name | \n”;$name | \n”;
| address | \n”;$address | \n”;
print $output
However, using quotes to generate XHTML output is inconvenient for the following reasons:
♦ The $output variable must be initialized. Before adding anything to the $output variable, give it an initial null value.
♦ You must repeatedly concatenate data onto the $output variable. The .= operator allows me to append something to a string variable.
♦ All quotes must be escaped. Because double quotes indicate the end of the string, all internal double quotes must be preceded with the back-slash (\).
♦ Every line must end with a newline (\n) sequence. PHP creates XHTML source code. Your PHP-derived code should look as good as what you write by hand, so you need to preserve carriage returns. This means you need to end each line with