A few helpful string and array PHP functions

iD Tech in action

I've been working a great deal with vBulletin, our forums system, and in doing so have had to brush on my PHP. Through the course of preparing the system for the summer I've come across a few PHP functions that I was not aware. Here's a few that you may not know about:

implode: takes an array of strings and concatenates them, using $glue as a separator.

Example: string implode (string $glue, array $pieces)
$before = array('one', 'two', 'three');
$after = implode(",", $before);
echo $after;

Produces:one,two,three

explode: does the exact opposite of implode - it takes a delimiter separated string and transforms it into an array.

Example: array explode (string $delimiter, string $string [, int $limit= -1])
$before = "one,two,three";
$after = explode(",", $before);
var_dump $after;

Produces:
array('one', 'two', 'three')

var_dump: unlike a typical echo, which will just print "Array" for arrays, var_dump will traverse and print the contents of the array.

Example: void var_dump (mixed $expression [, mixed $expression [, $... ]])
$before = "one,two,three";
var_dump $before;

Produces:
array(3) {
[0]=>string(1) "one"
[1]=>string(1) "two"
[2]=>string(1) "three"
}

Of course, if you use PHP any more than casually then you already know these functions. But if you frequently jump around among web technologies you might find these useful.

A photo of Ryan

Ryan manages blog content at iD Tech, starting with the company in 2008. He earned his MBA from Santa Clara University after obtaining his Bachelor’s degree from Arizona State. Connect on LinkedIn!

Get Brochure