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?

4 of 6 people (67%) answered Yes
Recently 2 of 2 people (100%) answered Yes

Entry

How do conditionals work?
How can I include an if statement inside a command?

Jan 27th, 2000 22:42
Nathan Wallace, Egon Schmid, Rasmus Lerdorf


Here is a "Conditionals for Dummies" summary:

It works like this:

  ( (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" );

Conditionals are not just for confusing grandmothers, though.  As per
Cameron's example, sometimes it is very handy to be able to check a
simple
condition in the middle of a larger expression.