Click to See Complete Forum and Search --> : Code explanation
EJMAES1973
12-25-2007, 01:57 AM
Possible a stupid question, but is the following code like an if statement? The only parts that confuse me are the ? and : If anyone could explain that I would be grateful. Thank you.
(!empty($_GET['pn'])) ? preg_replace('/[^0-9]/', '', $_GET['pn']) : null ;
tfk11
12-25-2007, 03:59 AM
It's similar yes.
If the expression preceding the question mark evaluates to true then the value of the expression after the question mark is returned else the value of the expression after the colon is returned.
true === (true) ? (true) : (false);
false === (false) ? (true) : (false);
EJMAES1973
12-25-2007, 08:19 AM
Ok, Thank you. Does this have a specific name, or is it just kind of there?
ZeroKilled
12-25-2007, 08:49 AM
is named as ternary conditional expression, however is commonly named as ternary operator. for more reference http://en.wikipedia.org/wiki/%3F:
EJMAES1973
12-25-2007, 09:06 AM
Alright, thank you for the help.
NogDog
12-25-2007, 09:44 AM
Also see the manual: http://www.php.net/ternary. (Scroll down 2-3 screens and you'll find the "Ternary Operator" heading.)