PHP conditionals, brackets needed?

前端 未结 7 2135
深忆病人
深忆病人 2020-11-28 05:55

I was just browsing a forum and someone asked about a PHP file they had found on the web. It has several spots like this in the code:

if ($REMOTE_ADDR == \"\"

相关标签:
7条回答
  • 2020-11-28 06:42

    To go into a little more detail, the reason that the braces are optional is that the syntax looks like:

    if(CONDITION) BLOCK
    [elseif(CONDITION) BLOCK]
    [else BLOCK]
    

    BLOCK can be a single statement:

    foo();
    

    or it can be a brace-enclosed group of statements:

    {
        foo();
        bar();
    }
    
    0 讨论(0)
  • 2020-11-28 06:43

    Braces (not brackets) are optional in PHP, as in most C-like syntax. Maybe you're thinking of Perl; they're required there, for that form of if syntax.

    The colon thing is an alternate control structure form that PHP supports. I hate it, but some people (particularly template system designers, apparently) love it.

    0 讨论(0)
  • 2020-11-28 06:51

    Yes, excluding the braces is allowed, although many times I have heard 2 reasons for not using that syntax:

    1. It's harder to read. Less obvious to another programmer.
    2. If you ever wany to add something inside the if, then you need to add the braces which is harder after then when you're first coding since most editors will add the closing brace for you.

    Also, yes, the colon syntax is valid. The alternatives can be found here: http://php.net/manual/en/control-structures.alternative-syntax.php

    0 讨论(0)
  • 2020-11-28 06:53

    9 years on and I'm surprised no-one's mentioned the ternary operator:

    $ip = ($REMOTE_ADDR == "") ? "no ip" : getHostByAddr($REMOTE_ADDR);
    

    Much clearer for assignment IMHO - because it leads out with the variable being assigned to, as for usual variable assignment.

    0 讨论(0)
  • 2020-11-28 06:57

    you can do if else statements like this:

    <?php
    if ($something) {
       echo 'one conditional line of code';
       echo 'another conditional line of code';
    }
    
    
    if ($something) echo 'one conditional line of code';
    
    if ($something)
    echo 'one conditional line of code';
    echo 'a NON-conditional line of code'; // this line gets executed regardless of the value of $something
    ?>
    



    and then you can also write if - else in an alternate syntax:

    <?php
    if ($something):
       echo 'one conditional line of code';
       echo 'another conditional line of code';
    elseif ($somethingElse):
       echo 'one conditional line of code';
       echo 'another conditional line of code';
    else:
       echo 'one conditional line of code';
       echo 'another conditional line of code';
    endif;
    ?>
    



    with the alternate syntax you can also fall out of parsing mode like this:

    <?php
    if ($something):
    ?>
    one conditional line of code<br />
    another conditional line of code
    <?php
    else:
       echo "it's value was: $value<br />\n";
    ?>
    another conditional line of code
    <?php
    endif;
    ?>
    

    But this gets really messy really fast and I won't recommend it's use (except maybe for template-logic).



    and to make it complete:

    <?php
    $result = $something ? 'something was true' : 'something was false';
    echo $result;
    ?>
    
    equals
    
    <?php
    if ($something) {
       $result = 'something was true';
    } else {
       $result = 'something was false';
    }
    echo $result;
    ?>
    
    0 讨论(0)
  • 2020-11-28 07:00

    In my opinion

    if ($REMOTE_ADDR == "") $ip = "no ip"; else $ip = getHostByAddr($REMOTE_ADDR);
    

    is valid, but much harder to read than:

    if ($REMOTE_ADDR == "") {
        $ip = "no ip"; 
    } else {
        $ip = getHostByAddr($REMOTE_ADDR);
    }
    
    0 讨论(0)
提交回复
热议问题