note 119595 modified in control-structures.do.while by pollita
| From: | pollita@php.net | Date: | Wed, 26 May 2021 17:53:30 +0000 |
| Subject: | note 119595 modified in control-structures.do.while by pollita | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-216435@lists.php.net to get a copy of this message | ||
The last example on this page is simply abuse of the
break keyword. Also, the
suggestion to use goto if you don't understand the abuse of break is
unsettling. (See the manual page for goto for more than enough reasons not to use it.)
The final example is generally better expressed using a typical if-else statement.
<?php
if ($i < 5) {
echo "i is not big enough";
} else {
$i *= $factor;
if ($i >= $minimum_limit) {
echo "i is ok";
/* process i */
}
}
?>
This version is easier to read and understand. And arguments for code golf are invalid as well as
this version is 3 lines shorter.
In conclusion, although you can certainly write code that abuses the break keyword, you
shouldn't in practice. Keep the code easy to read and understand for whoever inherits your
code. And remember, code is for humans not computers.
--was--
The last example on this page is simply abuse of the break keyword. Also, the
suggestion to use goto if you don't understand the abuse of break is
unsettling. (See the manual page for goto for more than enough reasons not to use it.)
The final example is generally better expressed using a typical if-else statement.
<?php
if ($i < 5) {
echo "i is not big enough";
} else {
$i *= $factor;
if ($i >= $minimum_limit) {
echo "i is ok";
/* process i */
}
}
?>
This version is easier to read and understand. And arguments for code golf are invalid as well as
this version is 3 lines shorter.
In conclusion, although you can certainly write code that abuses the break keyword, you
shouldn't in practice. Keep the code easy to read and understand for whoever inherits your
code. And remember, code is for humans not computers.
https://php.net/manual/en/control-structures.do.while.php