TIL of returning true/false directly
POSTED ON:
TAGS: booleans
I've been guilty of this.
<?php
function checkIsYouth($age) {
if ($age >= 15 && $age <= 24) {
return true;
}
return false;
}
So let's break it down!
If the age is:
- between 15 - 24, return
true
- else return
false
A one-line way that expresses the same thing is:
<?php
function checkIsYouth($age) {
return $age >= 15 && $age <= 24;
}
This works because it's going to return either true
or false
anyways. Might as well lift it to the return statement.
via Amir Etemad - Some Tips For PHP developers
Related TILs
Tagged: booleans