TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   ternary operator (http://www.talkphp.com/absolute-beginners/4276-ternary-operator.html)

allworknoplay 05-14-2009 08:02 PM

ternary operator
 
Ok this is really getting me peeved...what am I doing wrong? The syntax looks just fine...but PHP is complaining..


PHP Code:

<? if($_SESSION['user_id']) ? 'Logout' 'Sign In'?>


I keep getting this error:

Parse error: syntax error, unexpected '?'

Village Idiot 05-14-2009 08:07 PM

This is why I advise people to stay away from ternary operators altogether, they are hard to read and even harder to debug.

allworknoplay 05-14-2009 08:31 PM

Quote:

Originally Posted by Village Idiot (Post 24046)
This is why I advise people to stay away from ternary operators altogether, they are hard to read and even harder to debug.

Yeah it blows....

sketchMedia 05-14-2009 08:45 PM

PHP Code:

 <?php ($_SESSION['user_id']) ? 'Logout' 'Sign In'?>

No 'if' needed

Salathe 05-14-2009 08:48 PM

Quote:

Originally Posted by Village Idiot (Post 24046)
This is why I advise people to stay away from ternary operators altogether, they are hard to read and even harder to debug.

Hard to debug? Not really sure on that point. In this case the error was descriptive (if concise) and precise: the ? just isn't allowed there.

The problem is that you're mixing an if statement with a ternary operation when the former is not necessary.

Using a ternary operation
Note use of parentheses to avoid potential confusion from operator precedence differences.
PHP Code:

<?php

$text 
= ($_SESSION['user_id'] ? 'Logout' 'Sign In');

Using if
PHP Code:

<?php

$text 
'Sign In';
if (
$_SESSION['user_id'])
{
    
$text 'Logout';
}


allworknoplay 05-14-2009 09:25 PM

Thanks guys,

I get it now....I fixed it by using isset() but I don't need that either, I just took it out and it still worked...

Wildhoney 05-14-2009 10:19 PM

Also on the back of Salathe's mentioning of operator precedence, you could use the or to give you a different way of achieving the same thing. I can't say I've ever used it like this, but it's one to be aware of nonetheless.

php Code:
$szVar1 = $szVar2 or $szVar1 = 'Default';
var_dump($szVar1);

$szVar1 will equal "Default" if $szVar2 isn't set.

allworknoplay 05-14-2009 10:25 PM

Quote:

Originally Posted by Wildhoney (Post 24053)
Also on the back of Salathe's mentioning of operator precedence, you could use the or to give you a different way of achieving the same thing. I can't say I've ever used it like this, but it's one to be aware of nonetheless.

php Code:
$szVar1 = $szVar2 or $szVar1 = 'Default';
var_dump($szVar1);

$szVar1 will equal "Default" if $szVar2 isn't set.

Wow that's a new one....and it actually looks quite confusing..LOL....(but I get it...)

Salathe 05-14-2009 11:15 PM

If you're going to do that (don't!) then always wrap parentheses around the component parts even if they're not necessary for making things work!

That reminds of questions like: (you can ignore the capitals for this)
PHP Code:

// What are $a, $b, $c and $d?
$a FALSE OR TRUE;
$b FALSE || TRUE;
$c TRUE AND FALSE;
$d TRUE && FALSE


allworknoplay 05-14-2009 11:21 PM

Quote:

Originally Posted by Salathe (Post 24055)
If you're going to do that (don't!) then always wrap parentheses around the component parts even if they're not necessary for making things work!

That reminds of questions like: (you can ignore the capitals for this)
PHP Code:

// What are $a, $b, $c and $d?
$a FALSE OR TRUE;
$b FALSE || TRUE;
$c TRUE AND FALSE;
$d TRUE && FALSE


hmm....I'm about to look very bad but.....

$a = true
$b = false
$c = false
$d = true

Please be kind to me!!

:-/

maZtah 05-15-2009 06:04 AM

I'm also using the ternary operator alot. I think it's very well readable. I'm using it alot for HTML output like this:

HTML Code:

<li<?php echo ($i % 4 == 0 ? '" class=first" ' : ''); ?>>

wjgilmore 05-18-2009 04:58 PM

FWIW while initially ternary operators can be a bit confusing to read, over time I've found them indispensable primarily for reason of writing compact code. For instance, I regularly use a Zend Framework view helper which will return "his" if a user object's gender attribute is set to "m", and "her" if the attribute is set to "f". Using a ternary, I can make this determination within the view helper like so:

PHP Code:

return $user->gender == "m" "his" "her"

So while indeed the ternary may be a bit of a bear to understand at first, they can wind up removing literally hundreds of lines of code otherwise required by an if statement which ultimately performs an identical task.

Just my $0.02,
Jason
==
W. Jason Gilmore
Author "Easy PHP Websites with the Zend Framework"
http://www.easyphpwebsites.com/

Wildhoney 05-18-2009 05:27 PM

It may be worth adding that we can nest ternary operators.

It begins to get truly messy when you do this, and that's really when you should begin considering an if statement instead.

However:

php Code:
$szGender = 'M';
//$szGender = 'F';
//$szGender = '';

printf
(
    'Gender: %s',
    (
        !$szGender  ? 'Unknown'
                    : ($szGender == 'M'
                    ? 'Male'
                    : 'Female')
    )
);

Tanax 05-19-2009 08:18 AM

Quote:

Originally Posted by Wildhoney (Post 24287)
It may be worth adding that we can nest ternary operators.

It begins to get truly messy when you do this, and that's really when you should begin considering an if statement instead.

However:

php Code:
$szGender = 'M';
//$szGender = 'F';
//$szGender = '';

printf
(
    'Gender: %s',
    (
        !$szGender  ? 'Unknown'
                    : ($szGender == 'M'
                    ? 'Male'
                    : 'Female')
    )
);

Wow, I understand ternary operators, but here.. you just lost me :p Perhaps because I just woke up xD


All times are GMT. The time now is 06:14 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0