Page 1 of 1

Using If/Else Ternary Operators Rate Topic: -----

#1 jamillion  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 83
  • Joined: 23-July 08

Posted 29 November 2008 - 03:18 AM

A ternary operator can be used as a slightly faster, cleaner way to write simple if/else statements.

Let's take a basic if/else statement...

if ($apple == "fresh") {
  print "Yum yum! This apple is tasty.";
} else {
  print "Yuck! This apple tastes awful!";
}


The above statement works like this:

If $apple is fresh, print "Yum yum! This cake is tasty.". If it's not fresh, print "Yuck! This cake tastes awful!".

Using a ternary operator we can rewrite this statement in a much simpler form.

We would write it like:

$message = ($apple == "fresh") ? "Yum yum! This apple is tasty." : "Yuck! This apple tastes awful!";
print $message;


In case you're not sure how that works, there are 3 parts to the ternary assignment:

$variable = condition ? if true : if false

The part to the left of the ? is the condition we're testing.
The part between the ? and the : is what happens if the condition is true.
The part after the : is what happens if the condition is false.

Hopefully that makes sense and you can now start saving a bit of time by using ternary operators in your PHP scripting!

Is This A Good Question/Topic? 0
  • +

Replies To: Using If/Else Ternary Operators

#2 didgy58  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 246
  • Joined: 23-October 07

Posted 05 December 2008 - 09:59 AM

just a quick question out of interest to make sure i understand this, in the second if statment you have written you have

$message = ($apple = "fresh") ? "Yum yum! This apple is tasty." : "Yuck! This apple tastes awful!"



you are comparing apple with a single = is this correct? or should it still be a == as i always thought that = is setting a variable?
Was This Post Helpful? 0
  • +
  • -

#3 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 541
  • View blog
  • Posts: 1,708
  • Joined: 08-November 08

Posted 07 December 2008 - 08:51 PM

It should actually be $apple == "fresh", otherwise it would set $apple's value to "fresh" and return true, as it would otherwise.
Was This Post Helpful? 0
  • +
  • -

#4 jamillion  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 83
  • Joined: 23-July 08

Posted 09 January 2009 - 12:13 PM

View Postdidgy58, on 5 Dec, 2008 - 08:59 AM, said:

just a quick question out of interest to make sure i understand this, in the second if statment you have written you have

$message = ($apple = "fresh") ? "Yum yum! This apple is tasty." : "Yuck! This apple tastes awful!"



you are comparing apple with a single = is this correct? or should it still be a == as i always thought that = is setting a variable?

Yes you are right sorry for making that mistake it was silly and basic yet would make the whole script blow up in your face
Was This Post Helpful? 0
  • +
  • -

#5 clickfanatic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-January 09

Posted 11 January 2009 - 05:41 AM

Im a php newbie and this indeed it very useful. Though I suggest the author to make the proper correction to the tutorial. Anyways, thank you for this tutorial.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1