In PHP if & elseif is something i was always using when in fact a simple switch would have done the job and looked cleaner.
Looking at the following Switch and if else code, they do the same thing.
$status = 1;
switch ($status) {
case 0:
echo "Offline";
break;
case 1:
echo "Online";
break;
case 2:
echo "Away";
break;
} $status = 1;
if($status = 0){
echo "Offline";
} elseif ($status = 1) {
echo "Online";
} else {
echo "Away";
} We can minify the if else, although it can get confusing especially if you come to edit it in the future.
$status = 1;
if($status = 0){echo "Offline";} elseif ($status = 1) {echo "Online";} else {echo "Away";}; Its going to come down to how many options you will have, up to 3 different outcomes in an if else seems to be the limit for me, more than that and a switch just seems cleaner and easier to look at and edit rather than a massive nest of if & elseif.
A drained and empty Kennington reservoir images from a drone in early July 2024. The…
Merrimu Reservoir from drone. Click images to view larger.
Some early September spring shots from a bushwalk, It is still too early for wildflowers.
I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…
If you ever needed a simple solution in Laravel to delete an entire directory and…
How to enforce only one user to register or be registered in a Laravel web…