NYCPHP Meetup

NYPHP.org

[nycphp-talk] What are ? and :

Michael Sims jellicle at gmail.com
Fri Sep 30 12:58:27 EDT 2005


On Friday 30 September 2005 12:36, Aaron Fischer wrote:

> The symbols in question are ? and :

?: is the "ternary operator".  Google for that and you should get plenty of 
information.

http://ca.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

A ? B : C

can be roughly translated as "test A, and pick B or C depending on how A's 
test came out".

> 1)  $argv = isset($_SERVER['argv']) ? $_SERVER['argv'] : NULL;

So this is saying if the _SERVER variable argv is set, then set the local 
variable argv with that value; otherwise set it to NULL.  The more direct 
approach:

$argv = $_SERVER['argv'];

will throw an error if $_SERVER['argv'] doesn't exist.  (Actually, it will 
throw a notice, not an error, and the script will continue to execute, and 
the functional effect will be very similar, but using the ternary operator 
is certainly more elegant.)


Michael Sims



More information about the talk mailing list