Thread: Is the PHP ternary operator broken?

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Is the PHP ternary operator broken?

    This C code does what I expect:
    Code:
    #include <stdio.h>
    int main(){
    	int x = 3;
    	int y = (x<3)?1:(x<4)?2:(x<6)?3:4;
    	printf("%d\n", y);
    	return 0;
    }
    It prints the number 2.

    This PHP code does not:
    Code:
    <?php
    $x = 3;
    $y = ($x<3)?1:($x<4)?2:($x<6)?3:4;
    echo $y."\n";
    ?>
    Instead I get 3

    It only seems to evaluate the last expression. Anyone know if this would be a bug, or if there is a reason for this?

    I dont like it

    Edit: found the answer, http://bugs.php.net/bug.php?id=25356. Guess I should have searched first...
    Last edited by mike_g; 07-16-2009 at 05:09 PM.

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    You have to nest it in proper parentheses to make it work.
    Even if it does work without the parentheses in other languages, its always better to use them as it makes the code more readable.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The code is so unreadable it doesn't surprise me the least if it failed.
    You are abusing the operator. At least some new lines will help.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    PHP's conditional operator is left-associative, C++'s is right-associative.
    PHP: Operator Precedence - Manual
    See in particular the first comment on this page.

    Right-associative makes a lot more sense, but you can't change such a thing now. So yeah, PHP is broken. But so is any code where the ?: fixity is relevant.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I wouldn't say it is broken, just doesn't operate like other languages. After years of PHP programming I can honestly say I haven't had that particular issue cause a problem. What is much more likely to cause a problem is:
    Code:
    $status = 'You ' . $loggedin ? 'are' : 'are not' . ' logged into the system';
    Due to the precedence of . over ?: which of course confuses new PHP programmers because:
    Code:
    echo 'You ', $loggedin ? 'are' : 'are not', ' logged into the system';
    is just fine.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    The classic problem of getting too comfortable with one language. All of the sudden, the logical portion of your brain perceives "Doesn't work like my primary language" to be equivalent to "Doesn't work correctly."
    Sent from my iPadŽ

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Is that in reference to me or to the OP, Sly?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    The OP. In fact, I didn't read the entire thread too clearly to even notice your statement. Regardless, as stated, these issues don't come up in any language if the programmer correctly uses parenthesis to explicitly specify operator precedence and/or breaks up statements into more readable, less error-prone segments. It's certainly reasonable to express preference to one language's method over another, however, in a business where we don't always get to choose what language we work in professionally, it's highly recommended to accept alternatives and simply recognize the different simply as such.
    Last edited by SlyMaelstrom; 07-18-2009 at 01:26 AM. Reason: Incorrect tense in sentence.
    Sent from my iPadŽ

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And it's not like PHP did anything except inherit the semantics of Perl.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    PHP's conditional operator is left-associative, C++'s is right-associative.
    Thanks, that makes sense now. Guess its not a bug then, but its still annoying.
    What is much more likely to cause a problem is:
    Cant say I have ran into that yet, and I suppose I shouldn't have to now.
    The classic problem of getting too comfortable with one language
    Maybe, but C-like languages should behave C-like IMHO.

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by mike_g View Post
    Maybe, but C-like languages should behave C-like IMHO.
    Well, in that case, so should programmers

    Do not nest the ternary operator. It's poor form. Use regular conditional logic and operators instead.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But how C-like is PHP? It takes its greatest inheritance from Perl, not C. Perl already did the conditional operator this way, so if you want to blame someone, blame Larry Wall. (awk and C, Perl's primary sources of inspiration, both group ?: from right to left.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    It is hard to tell where some of the inspiration for the C-like (vs generic stuff) came from. Some of it smells of Java, other C++, and very little plain C.

    But there are much more interesting ways to abuse the language than ?:

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Should be interesting to know if anyone has any memories of PHP prior to PHP 3. Zeev and Andi completely rewrote the parser and created the Zend Engine the next year. It's also with PHP 3 that I seem to think the ternary conditional operator was introduced.

    Before that PHP was mostly in the hands of Lerdorf and despite the syntax being similar to Perl, it was entirely written in C, exactly to replace Perl scripts. My guess is that the Perl influenced parser happens when the two Israeli friends take hold of the project. That's PHP 3.0.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PHP installation
    By ssharish2005 in forum Tech Board
    Replies: 8
    Last Post: 11-23-2007, 09:42 PM
  2. PHP on my Computer!
    By xxxrugby in forum Tech Board
    Replies: 4
    Last Post: 03-15-2005, 09:34 AM
  3. php script question (is this possible?)
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 12-30-2003, 09:20 PM
  4. PHP 4.3.0 released
    By codingmaster in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-30-2002, 07:40 AM
  5. including files in php
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 07-06-2002, 10:32 AM