Underused PHP functions

PHP has a lot of built-in functionality – and it continues to grow and grow (and improve), which makes it easy to overlook some of it and create custom solutions instead of tried and tested PHP functions. I decided to make a list – for one to just remind myself to use these functions more in the future. I will amend this list if there are new functions which seem really useful but underused by myself.

pathinfo

pathinfo has been in PHP since 4.0.3 yet it only got my attention in 2016. Using PHP usually involves some kind of “file processing”: often you want to find out the file extension of a file (for example for uploaded files), or the directory of a file, just from a given string which could be an absolute path.

pathinfo processes a “file location” into directory name, base name, extension and filename (base name without extension). Very handy in a lot of situations!

array_map

Processing all items of an array in a specific way is a routine task in PHP – for example making sure all elements are numbers, stripping html tags or removing unnecessary whitespaces. You can do that easily with foreach:

$a = [‘hello’,’world’,’ help ‘,’ bla’];

foreach($a as $key => $value) {
$a[$key] = trim($value);
}

This is okay – readable and explicit. But it is so much shorter with array_map:

$a = [‘hello’,’world’,’ help ‘,’ bla’];

$a = array_map(‘trim’, $a);

The explicit foreach loop still has its advantages, especially for complex logic – but for simple cases and routine processing like trim, strip_tags or similar functions array_map makes the source code much more concise and reduces the possibility for errors.

preg_quote

I use preg_replace a lot – it is just so easy to transform strings with it, and I like the regular expressions syntax. One difficult thing to keep track of are all the special characters in regular expressions, because I do not use all of them, and when creating some replacements you easily forget one (I forgot to “escape” the dot so many times because it is used everywhere – numbers, regular texts, etc. – and your regex expressions usually work just the same).

When inserting a string in a regular expressions which should not contain any special regex characters, preg_quote comes in very handy – you don’t have to do any manual escaping (no more [.] instead of just .) and it reduces the chances of simple errors and even security problems. I am not sure how other developers do they regular expressions, but I have used preg_quote very seldom and just put more work and responsibility on myself for no reason. Don’t do that, me!

Also don’t forget to provide the second argument (specifying the delimiter you are using in your regex).

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s