13.09.2016 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using Operators<br />

39<br />

Other Operators<br />

In addition to the operators we have covered so far, you can use several others.<br />

The comma operator (,) separates function arguments <strong>and</strong> other lists of items. It is<br />

normally used incidentally.<br />

Two special operators, new <strong>and</strong> ->, are used to instantiate a class <strong>and</strong> access class<br />

members, respectively.They are covered in detail in Chapter 6.<br />

There are a few others that we discuss briefly here.<br />

The Ternary Operator<br />

The ternary operator (?:) takes the following form:<br />

condition ? value if true : value if false<br />

This operator is similar to the expression version of an if-else statement, which is covered<br />

later in this chapter.<br />

A simple example is<br />

($grade >= 50 ? ‘Passed’ : ‘Failed’)<br />

This expression evaluates student grades to ‘Passed’ or ‘Failed’.<br />

The Error Suppression Operator<br />

The error suppression operator (@) can be used in front of any expression—that is, anything<br />

that generates or has a value. For example,<br />

$a = @(57/0);<br />

Without the @ operator, this line generates a divide-by-zero warning.With the operator<br />

included, the error is suppressed.<br />

If you are suppressing warnings in this way, you need to write some error h<strong>and</strong>ling<br />

code to check when a warning has occurred. If you have <strong>PHP</strong> set up with the<br />

track_errors feature enabled in php.ini, the error message will be stored in the global<br />

variable $php_errormsg.<br />

The Execution Operator<br />

The execution operator is really a pair of operators—a pair of backticks (``) in fact.The<br />

backtick is not a single quotation mark; it is usually located on the same key as the ~<br />

(tilde) symbol on your keyboard.<br />

<strong>PHP</strong> attempts to execute whatever is contained between the backticks as a comm<strong>and</strong><br />

at the server’s comm<strong>and</strong> line.The value of the expression is the output of the comm<strong>and</strong>.<br />

For example, under Unix-like operating systems, you can use<br />

$out = `ls -la`;<br />

echo ‘’.$out.’’;

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

Saved successfully!

Ooh no, something went wrong!