Specifications
Other Operators
In addition to the operators we have covered so far, there are a number of others.
The comma operator, , ,is used to separate function arguments and other lists of items. It is
normally used incidentally.
Two special operators, new and ->, are used to instantiate a class and to access class members,
respectively. These will be covered in detail in Chapter 6.
The array operators,
[], enable us to access array elements. They will be covered in Chapter 3.
There are three others that we will discuss briefly here.
The Ternary Operator
This operator, ?:, works the same way as it does in C. It takes the form
condition ? value if true : value if false
The ternary operator is similar to the expression version of an if-else statement, which is
covered later in this chapter.
A simple example is
($grade > 50 ? “Passed” : “Failed”);
This expression evaluates student grades to “Passed” or “Failed”.
The Error Suppression Operator
The error suppression operator, @, can be used in front of any expression, that is, anything that
generates or has a value. For example
$a = @(57/0);
Without the @ operator, this line will generate a divide-by-zero warning (try it). With the opera-
tor included, the error is suppressed.
If you are suppressing warnings in this way, you should write some error handling code to
check when a warning has occurred. If you have PHP set up with the
track_errors feature
enabled, the error message will be stored in the global variable $php_errormsg.
The Execution Operator
The execution operator is really a pair of operators: a pair of backticks (``) in fact. The back-
tick is not a single quote—it is usually located on the same key as the ~ (tilde) symbol on your
keyboard.
PHP will attempt to execute whatever is contained between the backticks as a command at the
command line of the server. The value of the expression is the output of the command.
Using PHP
P
ART I
32
03 7842 CH01 3/6/01 3:39 PM Page 32