Ternary Operator Question

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
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Ternary Operator Question

Post by toasty2 »

Lately, for short and sweetness I've been using the ternary operator for only caring about one of the two conditions (either just true or just false), like:

Code: Select all

empty($somevar) ? exit : false;
Is there anything wrong with that? If that's fine to do, what should I put as the do-nothing code...in that example I just put "false", what would you do?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You should only use ternary conditions if you have an outcome for when the condition is true and one for when it is false.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Looks like a bad habit to get into to me. All you need is 1 if statement, no else.

When I see that it makes me wonder what the rest of your code looks like.

You could use..

Code: Select all

if (empty($foo)) exit();
or even better..

Code: Select all

// force $foo to be set because [your reason here]
if (empty($foo))
{
    exit();
}
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

superdezign wrote:You should only use ternary conditions if you have an outcome for when the condition is true and one for when it is false.
I know thats its purpose, but it works if for one of the outcomes I just put "filler code" (that doesn't do anything). There isn't anything wrong with that is there?

I'm not opposed to if statements, but I like the shortness of ternary.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Yes
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

toasty2 wrote:I know thats its purpose
Then why use it otherwise?
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

Alright, fine. :evil: :wink:
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

toasty2 wrote:I'm not opposed to if statements, but I like the shortness of ternary.
The if method appears to be shorter than you ternary one.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

If you're interested in writing shortcuts to the IF operator, try boolean arithmetics

Code: Select all

# exit IF condition is true
empty($someVar) && exit;

Code: Select all

# exit UNLESS condition is true
empty($someVar) || exit;
The latter is widely known from the notorious "or die" idiom.

The key difference of shorcuts (including ternary) compared to the normal IF is that you cannot use any statements, only expressions.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Comes down to personal taste. Ternary can be useful in places but many people find it harder to read (no IF to read and recognise). I tend to use them only where I need to output one thing or another based on a variable. Most other cases I use a normal if. An example of the use of the ternary below.

Code: Select all

echo $count.' User'.($count==1 ?  '' : 's').' have done this';
rather than

Code: Select all

if ($count==1) {
echo $count.' User have done this';
} else {
echo $count.' Users have done this';
}
In the example being given I would use

Code: Select all

if (empty($foo)) exit(); // exit when foo empty
Post Reply