Today I Learned - Rocky Kev

TIL of returning true/false directly

POSTED ON:

TAGS:

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:

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:

TIL Double Exclamation shorthand for booleans

JavaScript (and TypeScript) lets you convert a non-Boolean value to Boolean using the double exclamation shorthand. '!!'

TIL a easier way to check for multiple OR statements

Essentially, it's turning a multiple conditional into a array, then array to see if it matches.

TIL of returning true/false directly

return true or false directly bruh