PHP Conditionals, Brackets Needed

PHP conditionals, brackets needed?

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;
?>

PHP If / Else Statements Without Brackets

That is acceptable across version and will work for as long as you want to do only one thing within each control block. Using brackets does help to make it easier to track where blocks start and end, as well as future proofing for when you need to add just one more statement.

This post has a good summary of the various options for if/else blocks in PHP.

In php, should curly brackets be used even in short conditional statements?

I have seen way too many mistakes by people who do not use the curly brackets and then, later on, add an additional command and forget to wrap it.

My attitude is to wrap it for readability and future extensibility.

PHP bracket less IF condition not accepting more than one statement

This is how php works. If you don't put brackets around your if statement only the next statement is in the if block all other follow up statements are outside of it. But since you have a else block after it you will get a error.

(BTW: You make an assignment in the if block, so this will be always true)

Look at these 2 examples:

if($this->reel3 = 1)
parent::addCash($this->$bet*2); //In the if statement
print(parent::getCash()); //Outside the if statement
else

Same as:

if($this->reel3 = 1) {
parent::addCash($this->$bet*2);
}
print(parent::getCash());
//^^^^^ I think here it's more clear to see that this will give you a error, since it's between the if and else block which is not allowed
else { }

For more information about the control structure if see the manual: http://php.net/manual/en/control-structures.if.php

PHP - If/else, for, foreach, while - without curly braces?

When you omit the braces it will only treat the next statement as body of the condition.

if ($x) echo 'foo';

is the same as

if ($x) { echo 'foo'; }

but remember that

if ($x)
echo 'foo';
echo 'bar';

will always print "bar"

Internally it's the other way around: if will only look at the next expression, but PHP treats everything in {} as a single "grouped" expression.

Same for the other control statements (foreach, and so on)

Are brackets required at the end for if-else statements?

Is this a new php standard?

Not really. It totally depends on the user. You can use it in your code if you want -- it's your coding style. However, the Zend Coding standard requires you to use braces for all if, else, elseif constructs:

From their documentation:

PHP allows statements to be written without braces in some circumstances. This coding standard makes no differentiation- all "if", "elseif" or "else" statements must use braces.


I understand that not using brackets can make it a little more confusing, but I think in some cases it looks elegant

It's a good idea to use braces if your if blocks includes multiple conditions. Say, for example:

if ($auth) {
doStuff();
anotherOne();
}

However, if your code only has a single condition, you may write it without braces. For example:

if(is_numeric($n)) echo 'Foo';

For very short statements, it's not necessary to use braces, but if your code involves multiple conditions or you're writing code that another developer is going to re-use, then it's best to wrap them in braces. That way, there will not be any confusion when you look at the code in future.

whats the difference in parentheses in IF statements?

Parenthesis determine the order in which comparisons are made. Your example is a pretty simple one that doesn't need parenthesis at all, but look at something like this

if ($a == 0 || $b == 0 && $c == 0 || $dd == 0)

This is actually equivalent to

if ($a == 0 || ($b == 0 && $c == 0) || $dd == 0)

because && is evaluated first in PHP as it has a higher precedence than the ||

IN most cases, when you have complex conditionals, you want to make sure to use parenthesis, if not to get the order of operations right, then at least to make it clear to the code reader what you are trying to do.

PHP codes is brackets without any conditional statement

It doesn't affect memory management and is largely for structure and using code folding in editors.

As a bonus, it's also easy to exclude entire chunks by quickly adding if (false) in front.



Related Topics



Leave a reply



Submit