Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: Operators

  1. #1
    New Coder
    Join Date
    Apr 2005
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Operators

    Hi,

    What does this means? What will the output of $a?

    PHP Code:
    $a true true 2
    And what does a backticks (``) do?

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,058
    Thanks
    10
    Thanked 96 Times in 94 Posts
    $a=2 and I don't see how it could ever be anything else ... so I don't see the point of the code though I am probably missing something obvious ?

    backticks are essentially the same as system() call

    so
    PHP Code:
    <?
    system
    ('/etc/init.d/blah start');
    ?>
    and
    <?
    `/etc/init.d/blah start`;
    ?>
    both do the same thing.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    Your code appears to be a nested ternary.

    Normally, it would look like this:

    $a = (condition) ? value if condition= true : value if condition = false

    so it would look a bit like

    $a = true ? 2 : 1;

    which says if true then $a = 2 otherwise $a = 1;

    Now your code appears to be using another ternary as the conditon part of the second one... so its like this:

    $a = (condition) ? 1: 2;

    where the condition is the result of:
    true ? 0 : true;

    Now the condition part of the second ternary is just 'true' therefore it always returns the first value - 0 - which equates to false. So the condition returns false. So $a = 2

    Off the top of my head I cant think of any use for a nested ternary since the condition of the condition would have to evaluate to true or false, theres no need for the ternary.

    Also in sql statements ` backticks are sometimes used around field and tables names, although this is unecessary unless the names are reserved words - eg. if you had a column called 'order' you would have to write it as `order` so as not to be confused with the sql instruction ORDER BY

  • #4
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    This:
    PHP Code:
    $a true true 2
    is equivalent to this:
    PHP Code:
    if(true)
    {
        if(
    0)
        {
            
    $a 1;
        }
        else
        {
            
    $a 2;
        }
    }
    else
    {
        if(
    true)
        {
            
    $a 1;
        }
        else
        {
            
    $a 2;
        }

    Thus the code is evaluated as:
    PHP Code:
    $a = (true true) ? 2
    Probably more useful and how many other languages treat nested ternaries is this form:
    PHP Code:
    $a true : (true 2); 
    See also the manual for ternary operators.

    dumpfi
    Last edited by dumpfi; 07-16-2007 at 06:10 PM.
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • #5
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    Quote Originally Posted by dumpfi View Post
    This:
    PHP Code:
    $a true true 2
    is equivalent to this:
    PHP Code:
    if(TRUE){
        
    $a 0;
    }
    else
    {
        if(
    TRUE)
        {
            
    $a 1;
        }
        else
        {
            
    $a 2;
        }

    See also the manual for ternary operators.

    dumpfi
    Not quite - in your example $a would = 0 and never progress to the second part. If you run the code $a = 2;

    I dont think its actually possible to express as a simple if then else - this is the best I could come up with

    PHP Code:
    if(true)
    {
      
    $temp 0;
    }
    else
    {
      
    $temp true;
    }
    if(
    $temp)
    {
      
    $a 1;
    }
    else
    {
      
    $a=2;


  • #6
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    except it isn't. As firepages said, the result will be 2, with brackets it's:
    PHP Code:
    (true true) ? 
    which is a bit messy to express in longer notation:
    PHP Code:
    if(true) {
      
    $first_bit 0;
    }
    else {
      
    $first_bit true;
    }
    if(
    $first_bit) {
      
    $a 1;
    }
    else {
      
    $a 2;

    It's still fairly pointless code though.
    Last edited by GJay; 07-16-2007 at 06:03 PM. Reason: beaten :)
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com


  •  

    Posting Permissions

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