Blog & News

A few helpful string and array PHP functions

girls working at computer coding laughing

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.

  • Steve Davis

    Is it possible to include a string an an array element?

    Example:
    $key_words[0] = [‘password’];
    $key_words[1] = [‘required training’];
    $key_words[2] = [‘will not be dismissed’];

    So that [1] = required training
    [2] = will not be dismissed

    TIA for any assistance you can provide.

Popular Posts

Channels

Authors

About iD Tech

iD Tech is the #1 tech camp on the planet, with 150+ locations nationwide. Kids and teens learn to code, design video games, produce videos, mod Minecraft, create with Roblox, engineer robots, model and print 3D characters, work with AI and machine learning, build laptops, learn about cybersecurity, and more!

We've bet our reputation on recruiting the top instructors in the country. Our 8:1 student-to-instructor average ensures customized learning with the smallest class sizes, leading to "a-ha moments" and awesome outcomes.

Show all iD Tech Locations Show all iD Tech Courses