Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

instantiated class; the right-hand side is the name of a function or variable inside the
class.


Listing 2.10 Using Variables to Name Variables


<?
//set variables
$var_name = "myValue";
$myValue = 123.456;
$array_name = "myArray";
$myArray = array(1,2,3);


//prints "123.456"
print($$var_name. "
\n");


//prints "$myValue"
//perhaps not what you expect
print("$$var_name
\n");


//prints "123.456"
print("${$var_name}
\n");


//prints "3"
print(${$array_name}[2]. "
\n");
?>


The => operator is used in declaring arrays, discussed in Chapter 5. When creating an
array with the array statement, you may specify the index for an element with the =>
operator. The left-hand side of the operator is the index and the right-hand side is the
value. This operator is also used by the foreach statement in much the same way.


The? operator is equivalent to an if statement. It is called a tertiary operator, because it
takes three parameters: an expression that is evaluated to be TRUE or FALSE, an expression
that's evaluated if the first is true, and an expression that's evaluated if the first is false. A
complete discussion of the? operator appears in Chapter 3, "Control Statements."


The @ operator suppresses any error messages when it precedes an expression. Normally
when a built-in function encounters an error, text is sent directly to the browser.
Sometimes this is just warning text. If you want to suppress any error or warning
messages, place @ directly before the name of the function. You may also place @ before
an expression if you anticipate an error condition, such as division by zero. Error
messages may also be suppressed for all functions in a script with the error_reporting
function.


Assignment Operators


There really is only one assignment operator, but PHP offers a handful of shortcut
operators for combining assignment with another operator. Table 2.10 lists all the


Assignment Operators......................................................................................

Free download pdf