Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP conditionals, brackets needed?

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?

like image 967
Levi Avatar asked Dec 19 '08 15:12

Levi


People also ask

Do you need brackets in PHP?

Now, PHP has the Linting feature inbuilt so this extension is not necessary. If you do not like the inbuilt Linting of Brackets, then you can use this tool.

What are PHP conditional statements?

PHP Conditional Statementsif statement - executes some code if one condition is true. if...else statement - executes some code if a condition is true and another code if that condition is false. if... elseif...else statement - executes different codes for more than two conditions.


2 Answers

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; ?> 
like image 180
Jacco Avatar answered Sep 23 '22 09:09

Jacco


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(); } 
like image 32
chaos Avatar answered Sep 19 '22 09:09

chaos