faqts : Computers : Programming : Languages : PHP : kms : General

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

56 of 59 people (95%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

What is a ternary expression?
What is this ? : syntax?

Jun 16th, 1999 07:00
Nathan Wallace, Rasmus Lerdorf, Chad, Egon Schmid


A ternary expression is a shortened form of an if statement:

  ( (condition) ? "true" : "false" )

If the condition is true, then the above line evaluates to "true" and
if the condition is false, it evaluates to "false".

Another way to look at it.  Take this very normal example:

  if ($a==1) {
      print "a is one";
  }
  else {
      print "a is not one";
  }

Even your grandmother could probably tell you what this did.  You can
rewrite this with a conditional to confuse your grandmother, like this:

  print ( ($a==1) ? "a is one" : "a is not one" );

http://www.php.net/manual/expressions.php3