13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Accessing Form Variables<br />

27<br />

This is equivalent to the first statement shown in this section. Either format is valid, <strong>and</strong><br />

which one you use is a matter of personal taste.This process, replacing a variable with its<br />

contents within a string, is known as interpolation.<br />

Note that interpolation is a feature of double-quoted strings only.You cannot place<br />

variable names inside a single-quoted string in this way. Running the following line of<br />

code<br />

echo ‘$tireqty tires’;<br />

simply sends “$tireqty tires” to the browser.Within double quotation marks,<br />

the variable name is replaced with its value.Within single quotation marks, the variable<br />

name or any other text is sent unaltered.<br />

Variables <strong>and</strong> Literals<br />

The variables <strong>and</strong> strings concatenated together in each of the echo statements in the<br />

sample script are different types of things.Variables are symbols for data.The strings are<br />

data themselves.When we use a piece of raw data in a program like this, we call it a literal<br />

to distinguish it from a variable. $tireqty is a variable, a symbol that represents the<br />

data the customer typed in. On the other h<strong>and</strong>, ‘ tires’ is a literal.You can take<br />

it at face value.Well, almost. Remember the second example in the preceding section?<br />

<strong>PHP</strong> replaced the variable name $tireqty in the string with the value stored in the<br />

variable.<br />

Remember the two kinds of strings mentioned already: ones with double quotation<br />

marks <strong>and</strong> ones with single quotation marks. <strong>PHP</strong> tries to evaluate strings in double<br />

quotation marks, resulting in the behavior shown earlier. Single-quoted strings are treated<br />

as true literals.<br />

There is also a third way of specifying strings using the heredoc syntax (

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!