Newbie Question - The ($a = $b ? $c : $d) thing...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Envex
Forum Newbie
Posts: 13
Joined: Fri Sep 23, 2005 2:51 pm

Newbie Question - The ($a = $b ? $c : $d) thing...

Post by Envex »

Hey guys,

I feel kind of stupid for asking this, because believe it or not, I am a pretty decent developer. Anyway, I was wondering, could someone please explain this to me:

Code: Select all

$a = $b ? $c : $d
How exactly does that work? I've been searching through the PHP manual and Google, but couldn't find anything. There's nothing about it in the Operators section of the PHP5 manual, and trying to search for "?" and ":" aren't exactly the greatest search terms. Any help?

I'm just starting to break into PHP, and it's a great language with alot of things I really like, but the one thing I don't is that I can't do things like:

Code: Select all

$name = 'John' if $var == 12;
Instead, it seems as though I have to:

Code: Select all

if ($var == 12) { $name = 'John'; }
else { $name = $var; }
Anyway, any help would be greatly appreciated.

Matt
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

you have met the tenary operator I do believe it is called. either way, your statement essentially reads "if a is equal to b then c, else d." or

Code: Select all

if ($a=$b)
{
 $c;
}
else
{
 $d;
}
what you wrote is just a more compact way of saying it.
RobertPaul
Forum Contributor
Posts: 122
Joined: Sun Sep 18, 2005 8:54 pm
Location: OCNY

Post by RobertPaul »

It's called the Ternary Operator: http://php.net/language.operators.comparison

Instead of writing:

Code: Select all

if ($foo > $bar) {
	$var = 1;
} else {
	$var = 2;
}
You can write:

Code: Select all

$var = ($foo > $bar ? 1 : 2);
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

hmm.he had some typos, i didn't even notice:-D and I had a typo in the term, it'll be aight.
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

To which programming language does this corresponds to?

Code: Select all

$name = 'John' if $var == 12;
It seems very unclear, I mean - using If statements this way means that you must have an else option at any case
(with comparison to the PHP equivilant you specified in your post.)
Envex
Forum Newbie
Posts: 13
Joined: Fri Sep 23, 2005 2:51 pm

Post by Envex »

That would be Perl. :)

Yeah, sorry, I had a pretty bad example in my first post. It does come in handy though. For example:

Code: Select all

foreach $var (@whatever) { 
      next unless $var > 0;
}

$amount = 0 if !$amount;
That kind of thing you can't do in PHP. :) And thanks for all of your replies. It's appreciated.

Matt
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Sure, that's why you're in this forum.

Maybe you could explain what that Perl code does?
Envex
Forum Newbie
Posts: 13
Joined: Fri Sep 23, 2005 2:51 pm

Post by Envex »

That Perl code itself does absolutely nothing. I was just trying to demonstrate one of the differences between the two that I like Perl better for.

For example, in Perl you can do:

Code: Select all

$amount = 0 if !$amount
Where as in PHP, you would have to use:

Code: Select all

if (empty($amount)) { $amount = 0; }
No big deal on a small scale, but when working with larger projects, it's nice to have that flexibility in Perl. :) I guess it's personal perference, and just a matter of getting used to, but for me, it makes the code alot cleaner and easier to read.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

you could do

Code: Select all

empty($amount) ? $amount=0 : ;
unless i'm mistaken.i don't know if you cna leave hte second option blank in a ternary operand..
Envex
Forum Newbie
Posts: 13
Joined: Fri Sep 23, 2005 2:51 pm

Post by Envex »

hehe, exactly why I originally posted this topic. :)

I seen that ternary operator in Perl, but never really bothered with it, because I had no real use for it. But now with PHP, I have a use for it. :)
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

Another note, I leave out brackets for simple things. e.g.

Code: Select all

if (empty($amount)) $amount = 0;
Just makes things that much easier to read. ;)

Though in this case, a ternary operator looks better in my opinion.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

using a ternary in the following Perl case is silly

Code: Select all

$amount = 0 if !$amount
a regular if is best. It's easier to read for most users.

And because I've seen far too many coding mistakes happen from not using braces around the block statement following an if or other flow control operator, I have to recommend that you always always always surround them using braces. Even for the one-liners.
Post Reply