+ Reply to Thread
Results 1 to 6 of 6

Thread: The Ternary operator in PHP

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    The Ternary operator in PHP

    The Ternary comparison operator is an alternative to the if-else statement, it's shorter and could be used to easily do a significant difference depending on a condition without the need of a if statement. The Ternary operator is made for two different actions, one if the condition is true, the other one if it's false but with nested Ternary operators we can use it like an if-elseif.else statement(more to that later). The syntax of a Ternary operator looks like this:


    Code:
    (condition) ? TRUE FALSE 
    The Ternary operator will return either the first value(if the condition were true) or the second value (if the condition were false), so here comes a simple example on how we can use it:

    Code:
    $siteFeedback = ($Site == "codecall") ? "Awesome" "Unknown"
    So what the example does is checking if $Site is equals to codecall, if it is "Awesome" will be stored in $siteFeedback but if it's not "Unknown" will be stored.

    The above example would look like this with an if-else statement:

    Code:
    if ($Site == "codecall") {
        
    $siteFeedback "Awesome";
    }else{
        
    $siteFeedback "Unknown";



    Since the Ternary operator returns a value we can use it for everything, not only storing different values in variables.

    Code:
    echo "The current pupil " . (($pupils[$i] >= 15) ? "did" "didn't") . " pass the test"
    In the above example we're testing inside the echoing of a string if it should be "did" or "didn't" depending on how much points the pupil had. In this case we had to put the Ternary operator inside parentheses to make it work. In the example you saw how simple we could alter a string with a Ternary operator inside it. With an if-else statement we would have to first store "did" or "didn't" in a variable and then put it there instead. Or we could have wrote the same string twice with only the difference between "did" and "didn't" but that wouldn't have been so good if we wanted to alter the base string, then we would have to do it twice.





    Nested Ternary operators

    we can also nest them since they returns a value and each Ternary operator choose between two values so then we can only replace one (or both) value by another Ternary operator , like so:

    Code:
    echo ($X $Y) ? "X is greater then Y" : (($X $Y) ? "Y is greater then X " "X and Y has the same value."); 
    But this could be pretty hard to read so if you use nested Ternary operators you should find a good way to write it, an example could be writing it like this:

    Code:
    echo ($X $Y)  ?   "X is greater then Y" 
       
    : (($X $Y) ?   "Y is greater then X" 
       
    :                "X and Y has the same value")
       ; 


    That was the end of the tutorial, I hope you liked it

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: The Ternary operator in PHP

    The Ternary operator is very handy but I think it is also harder to read. John wrote a blog about it somewhere. +rep!

  4. #3
    so1i's Avatar
    so1i is offline Programming Professional
    Join Date
    Sep 2009
    Location
    Aberystwyth, United Kingdom
    Posts
    309
    Rep Power
    0

    Re: The Ternary operator in PHP

    Nice tutorial. I think it's harder too, but good for people to be aware of! Makes code look a lot more confusing than it really is. +rep

  5. #4
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: The Ternary operator in PHP

    I'm not a big fan of the ternary operator, myself. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    Join Date
    Nov 2009
    Posts
    20
    Rep Power
    0

    Re: The Ternary operator in PHP

    yea i agree it is a little hard to read +rep though good tutorial

  7. #6
    technica's Avatar
    technica is offline Learning Programmer
    Join Date
    Oct 2009
    Posts
    64
    Rep Power
    0

    Re: The Ternary operator in PHP

    Here I understood the ternary operators which i used to see in many open source project code files.

    Thanks mate.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. insertion "<<" operator, extraction ">>" operator
    By jackson6612 in forum C and C++
    Replies: 2
    Last Post: 10-11-2011, 12:35 PM
  2. operator
    By Apprentice123 in forum C and C++
    Replies: 5
    Last Post: 10-29-2010, 01:46 PM
  3. new operator
    By jaiii in forum C and C++
    Replies: 5
    Last Post: 06-30-2010, 12:34 AM
  4. Operator <<
    By atod in forum C and C++
    Replies: 9
    Last Post: 01-27-2009, 09:24 AM
  5. << operator??
    By stack in forum Java Help
    Replies: 5
    Last Post: 07-18-2007, 05:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts