TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-14-2009, 08:02 PM   #1 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default 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 '?'
allworknoplay is offline  
Reply With Quote
Old 05-14-2009, 08:07 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

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

Village Idiot is offline  
Reply With Quote
Old 05-14-2009, 08:31 PM   #3 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
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....
allworknoplay is offline  
Reply With Quote
Old 05-14-2009, 08:45 PM   #4 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

PHP Code:
 <?php ($_SESSION['user_id']) ? 'Logout' 'Sign In'?>
No 'if' needed
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 05-14-2009, 08:48 PM   #5 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
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';
}
Salathe is offline  
Reply With Quote
Old 05-14-2009, 09:25 PM   #6 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

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...
allworknoplay is offline  
Reply With Quote
Old 05-14-2009, 10:19 PM   #7 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 05-14-2009, 10:25 PM   #8 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
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...)
allworknoplay is offline  
Reply With Quote
Old 05-14-2009, 11:15 PM   #9 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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
Salathe is offline  
Reply With Quote
Old 05-14-2009, 11:21 PM   #10 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
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!!

allworknoplay is offline  
Reply With Quote
Old 05-15-2009, 06:04 AM   #11 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

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" ' : ''); ?>>
maZtah is offline  
Reply With Quote
Old 05-18-2009, 04:58 PM   #12 (permalink)
The Wanderer
 
Join Date: May 2009
Location: Columbus, Ohio
Posts: 10
Thanks: 0
wjgilmore is on a distinguished road
Default

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/
wjgilmore is offline  
Reply With Quote
Old 05-18-2009, 05:27 PM   #13 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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')
    )
);
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 05-19-2009, 08:18 AM   #14 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
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
__________________
Tanax is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using ISSET()...How does this line of code work? Dave Absolute Beginners 10 06-15-2008 12:15 AM
Fatal error: [] operator not supported for strings in /.... pipesportugal General 2 06-05-2008 09:00 PM
PHP6 Wishlist Wildhoney General 34 11-30-2007 07:27 PM
Identity and Equivalence Karl Absolute Beginners 1 09-13-2007 12:40 AM


All times are GMT. The time now is 11:22 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design