29 April 2015

Useful PHP Functions

PHP is one of if not the most popular language for web development. Its popularity may be a result of its ability to be simply mixed with HTML. Developers can easily open up a PHP block inside any HTML tag allowing dynamic content to be embedded pretty much anywhere with little effort. PHP is generally an easy language to learn and is full of useful functions that can help for most situations. Below I will highlight a few functions I use on a day to day basis.

substr($string,$start,$optionalLength);

Reference: http://php.net/substr

Substr returns a portion of a string. The first parameter is the string you wish to apply substr to. The second parameter defines where you wish to start. The last optional parameter defines how many characters of text you wish to return.

Substr can be useful if you need to find out if an image is a jpg or a png.
$fileName = substr(‘my-image.jpg’,-3,3); // $fileName = jpg

explode($delimiter,$string,$optionalLimit);

Reference: http://php.net/EXPLODE

Explode will turn a string into an array of strings formed by splitting the base string by the defined delimiter. This can be useful if you have a list of email addresses and you need to access them individually. The optional limit will define how many items to add to the array

$emails = “[email protected], [email protected], [email protected], [email protected]”;
$emailArray = explode(‘,’,$emails);

print_r($expression)

Reference: http://php.net/manual/en/function.print-r.php

print_r is easily in my top 5 most used functions list. I don’t generally use it for anything other than debugging in development but while developing I use it a lot. It’s useful when you have an array or object and you’re not quite sure what data is contained by said object. It will display the data allowing you to move forward with development without being blind to the data you are working with.

Share This Article