Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Question 3-1
The tag used to start PHP interpreting code is <?php ... ?>, which can be shortened to <? ... ?>.
Question 3-2
You can use // for a single line comment or /* ... */ to span multiple lines.
Question 3-3
All PHP statements must end with a semicolon (;).
Question 3-4
With the exception of constants, all PHP variables must begin with $.
Question 3-5
Variables hold a value that can be a string, a number, or other data.
Question 3-6
$variable = 1 is an assignment statement, whereas $variable == 1 is a comparison operator. Use $variable = 1 to set the value of $variable. Use $variable == 1 to find out later in the program whether $variable equals 1. If you mistakenly use $variable = 1 where you meant to do a comparison, it will do two things you probably don’t want: set $variable to 1 and return a true value all the time, no matter what its previous value was.
Question 3-7
A hyphen is reserved for the subtraction operators. A construct like $current-user would be harder to interpret if hyphens were also allowed in variable names and, in any case, would lead programs to be ambiguous.
Question 3-8
Variable names are case-sensitive. $This_Variable is not the same as $this_variable.
Question 3-9
You cannot use spaces in variable names, as this would confuse the PHP parser. Instead try using the _ (underscore).
Question 3-10
To convert one variable type to another, reference it and PHP will automatically convert it for you.
Question 3-11
There is no difference between ++$j and $j++ unless the value of $j is being tested, assigned to another variable, or passed as a parameter to a function. In such cases, ++$j increments $j before the test or other operation is performed, whereas $j++ performs the operation and then increments $j.
Question 3-12
Generally, the operators && and and are interchangeable except where precedence is important, in which case && has a high precedence while and has a low one.
Question 3-13
You can use multiple lines within quotations marks or the <<< _END ... _END construct to create a multiline echo or assignment.
Question 3-14
You cannot redefine constants because, by definition, once defined they retain their value until the program terminates.
Question 3-15
You can use \' or \" to escape either a single or double quote.
Question 3-16
The echo and print commands are similar, except that print is a PHP function and takes a single argument and echo is a construct that can take multiple arguments.
Question 3-17
The purpose of functions is to separate discrete sections of code into their own, self-contained sections that can be referenced by a single function name.
Question 3-18
You can make a variable accessible to all parts of a PHP program by declaring it as global.
Question 3-19
If you generate data within a function, you can convey the data to the rest of the program by returning a value or modifying a global variable.
Question 3-20
When you combine a string with a number, the result is another string.