35

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 == "") $ip = "no ip"; else $ip = getHostByAddr($REMOTE_ADDR);

I have always thought brackets are needed to enclose what you want to do if the condition is true. Is there some other alternative, such as if it is on the same line you don't?

There is also another line like this: if ($action != ""): mail("$adminaddress","Visitor Comment from YOUR SITE",

My instinct is to say this wouldn't work, but I also don't know if it is an outdated PHP file and it used to work?

halfer
  • 19,824
  • 17
  • 99
  • 186
Levi
  • 12,214
  • 14
  • 43
  • 47

7 Answers7

79

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;
?>
Jacco
  • 23,534
  • 17
  • 88
  • 105
  • 2
    Wow, I didn't know there were so many options for structuring php code. Thanks! – Levi Dec 19 '08 at 15:37
  • 2
    If you last example, you don't need the extra variable: echo $something ? 'something was true' : 'something was false'; – Darryl Hein Dec 19 '08 at 15:40
  • I know it works, but I think I read somewhere that it was a non-documented result (and thus could change in future version). Not sure though. – Jacco Dec 19 '08 at 15:46
  • Could you also use elseif for a single line of conditional code like this? if ($something) $result = 'something was true'; elseif ($something_else) $result = 'something else was true'; else $result = 'nothing is true'; – Adal Feb 23 '15 at 00:16
  • Can "elseif" actually leave the if condition blank? (i.e. Is "elseif:" works?) – AkiEru Feb 23 '15 at 08:48
  • note it's more readable to indent your one conditionnal line of code – Reign.85 Apr 10 '15 at 12:02
  • you need explain or add a: elseif(CONDITION) ...block like next response,.... because second block of your code not have condition and no specifi why shose some desition or part of code: if(): {echos} elseif: {echos} else: –  May 26 '15 at 16:11
  • I love 'colon' syntax, its reallly python-like, and to me it's much more easier to read with properly-indented code. Closing a brace early is much common error, at least for me, than forgetting an endif. – DDS Aug 20 '19 at 14:48
10

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();
}
chaos
  • 122,029
  • 33
  • 303
  • 309
2

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.

chaos
  • 122,029
  • 33
  • 303
  • 309
  • Thanks, it must have been that I learned Perl first and it must have been the stricter side of me thinking it was wrong. – Levi Dec 19 '08 at 15:36
2

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);
}
smartcoder
  • 129
  • 4
  • I like `if(statement) process; else process2;` all on separate lines. It looks just like the one with brackets except without the brackets. – JVE999 Jul 24 '14 at 04:00
2

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.

Velojet
  • 878
  • 11
  • 18
1

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

Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
  • --- 1. It's harder to read. Less obvious to another programmer. That's controversial. Many programmers like to "absorb" more code with less vertical scrolling (I'm not saying that you have to write several ;;; instructions on single line) This way you can see better the "bigger picture". But I agree, it's always better for debug mode. – PatlaDJ Dec 22 '12 at 12:27
-3
brackets are needed to enclose what you want to do if the condition is true

I can't think of any language that requires this

Scott Evernden
  • 39,136
  • 15
  • 78
  • 84