If the true or false clause of an if statement has only one statement, you do not need to use braces (also called "curly brackets"). This braceless style is dangerous, and most style guides recommend always using them.
if..else statements In an if...else statement, if the code in the parenthesis of the if statement is true, the code inside its brackets is executed. But if the statement inside the parenthesis is false, all the code within the else statement's brackets is executed instead.
As for me, curly braces serve as a substitution for concatenation, they are quicker to type and code looks cleaner. Remember to use double quotes (" ") as their content is parsed by PHP, because in single quotes (' ') you'll get the literal name of variable provided: <?
Curly braces indicates the block of statements in C programming language. The function in C has braces in its syntax. But one can put braces where logically related statements are written. A single statement in loop or if-else block need not be written within 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)
There are places where you can, but you never should.
Explicit is always better than implicit.
Who knows when you're going to have to go back and modify old code. It's so easy to miss that stuff and there's nothing gained by doing it.
It will work fine if you only have one argument inside!. But if you want to omit curly brace you can use colon and end. example:
if(a < 1 ) :
echo "a is less than 1";
else :
echo "a is greater than 1";
endif;
As have said you don't wanna learn about shorthand's and accepted answer gives good example about omitting curly braces
, but there is something to add. As you can see it's fine to omit curly braces
in case of if ($x) echo 'foo';
. There is nothing wrong with code, no performance or other issues and it is readable by other developers. And example also shows you that if you write
if ($x)
echo 'foo';
echo 'bar';
instead of
if ($x)
echo 'foo';
echo 'bar';
You can run into unwanted results where bar
is printed while you don't want it to be printed and if your code is full of such statements then it will make it harder for you to read your own code and even more harder for others to read it.
I don't wanna learn about shorthand's I just want to understand the conditions about when and where it is possible to omit the curly brackets.
These things are closely related so if you really want to understand where it is possible to omit curly brackets then that should be a must that you understand or are at least aware of shorthand's , have read
So my big question is: When can I omit the curly braces and in which structure/loop/function?
The curly brace is not required however, for readability and maintenance, many developers would consider it bad style not to include them. Previous 2 links should give you information needed to make your own decisions when you could omit curly brace. for example there is nothing wrong with following code snippets which all do exactly same thing.
With curly brace
if (PHP_VERSION_ID < 70000)
{
print "PHP >= 7.0 required yours is ";
print phpversion();
print "\n";
exit(1);
}
Is same as
if (PHP_VERSION_ID < 70000) :
print "PHP >= 7.0 required yours is ";
print phpversion();
print "\n";
exit(1);
endif;
Or you can use the dot operator
if (PHP_VERSION_ID < 80000)
(print "PHP >= 7.0 required yours is ") . (print phpversion()) . (print "\n") . exit(1);
And you can make use of the ternary conditional operator and even omit if
it self besides omitting curly braces
(PHP_VERSION_ID > 70000) ?: (print "PHP >= 7.0 required yours is ") . (print phpversion()) . (print "\n") . exit(1);
Since we only print we can shorten that and strip some print string functions
which were here to represent more than one function in statement without curly braces
(PHP_VERSION_ID > 70000) ?: (print "PHP >= 7.0 required yours is " . phpversion() . "\n") . exit(1);
As from php 7 we can use Null coalescing operator
(PHP_VERSION_ID > 70000) ?: null ?? (print "PHP >= 7.0 required yours is ".phpversion() . "\n") . exit(1);
As one can see that there is many ways you can get exactly the same result. That not only applies for this if
example but same can be practiced with structure/loop/function
. So there is no one answer for your big question. One should consider mainly following.
I omit curly braces in my PHP templates. E.g. you can use loops as follows:
<ul>
<?php foreach ($var as $value): ?>
<li><?php echo $value; ?></li>
<?php endforeach; ?>
</ul>
You can use it for simple things like:
if($a === true) continue;
But for some more complicated sub-conditions it may cause you problems:
$a = false;
$b = false;
if ($a === true)
if ($b === true)
echo 1;
else
echo 2;
With the code above, you would expect to see "2" as output, but you won't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With