Some Tips For PHP developers

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


At the first glimpse, this code looks ok, but there is an issue with the performance! In each iteration, the ‘count’ function will be executed and if you are having a large array it will cause a performance issue.

To fix this issue, you can easily calculate the count of the array before ‘for’ or inside for(). Have a look at this code:

A slick way to return a Boolean

We could just return the condition here!

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

Be careful when using strpos

0 and False are the same, you need to control the value and type both.

// 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';

In PHP 8, there is a new function str_contains() which returns True and False :)

Backend Developer, Flexitarian, Technology enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store