Optionality of Curly Braces in Conditions in PHP
In case there is only one expression inside the curly braces of if
or else,
these curly braces can be omitted. Let's say,
for example, we have this code with all the braces:
<?php
if ($test == 0) {
echo '+++';
} else {
echo '---';
}
?>
It can be shortened like this:
<?php
if ($test == 0) echo '+++'; else echo '---';
?>
Or like this:
<?php
if ($test == 0) {
echo '+++';
} else echo '---';
?>
You can also remove all braces, but format everything not in a line, but like this:
<?php
if ($test == 0)
echo '+++';
else
echo '---';
?>
Rewrite the following code in a shortened form:
<?php
if ($test == 0) {
echo 'yes';
} else {
echo 'no';
}
?>
Rewrite the following code in a shortened form:
<?php
if ($test == 0) {
echo 'yes';
}
?>