Page 1 of 1

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

Posted: Sat Sep 24, 2005 1:33 pm
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

Posted: Sat Sep 24, 2005 1:39 pm
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.

Posted: Sat Sep 24, 2005 1:42 pm
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);

Posted: Sat Sep 24, 2005 1:47 pm
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.

Posted: Sat Sep 24, 2005 2:04 pm
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.)

Posted: Sat Sep 24, 2005 2:44 pm
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

Posted: Sat Sep 24, 2005 2:58 pm
by pilau
Sure, that's why you're in this forum.

Maybe you could explain what that Perl code does?

Posted: Sat Sep 24, 2005 3:04 pm
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.

Posted: Sat Sep 24, 2005 3:07 pm
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..

Posted: Sat Sep 24, 2005 3:14 pm
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. :)

Posted: Sat Sep 24, 2005 3:25 pm
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.

Posted: Sat Sep 24, 2005 4:05 pm
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.