Some Tips For PHP developers

Tips for PHP developers to write shorter and high-performance code

For-Loop

A slick way to return a Boolean

return $age >= 15 && $age <= 24;

Be careful when using strpos

// This if condition not work because strpos() returns 0 and 0 is false
if (strpos('Foo Bar Baz', 'F')) {
echo 'Found';
}
// This will work properly.
if (strpos('Foo Bar Baz', 'F') !== false) {
echo 'Found';
}

Count() function in php 7.2

$count = [];
if(is_array($count) || $count instanceof Countable) {
echo count($count);
}

Backend Developer, Flexitarian, Technology enthusiast