School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,414 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,590 people online right now. Registration is fast and FREE... Join Now!




Using If/Else Ternary Operators

 
Reply to this topicStart new topic

> Using If/Else Ternary Operators

jamillion
Group Icon



post 29 Nov, 2008 - 02:18 AM
Post #1


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...

CODE
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:

CODE
$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!
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

didgy58
**



post 5 Dec, 2008 - 08:59 AM
Post #2
just a quick question out of interest to make sure i understand this, in the second if statment you have written you have
CODE


$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?
Go to the top of the page
+Quote Post

Valek
Group Icon



post 7 Dec, 2008 - 07:51 PM
Post #3
It should actually be $apple == "fresh", otherwise it would set $apple's value to "fresh" and return true, as it would otherwise.
Go to the top of the page
+Quote Post

jamillion
Group Icon



post 9 Jan, 2009 - 11:13 AM
Post #4
QUOTE(didgy58 @ 5 Dec, 2008 - 08: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
CODE


$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
Go to the top of the page
+Quote Post

clickfanatic
*



post 11 Jan, 2009 - 04:41 AM
Post #5
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.
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/7/09 11:25PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month