PHP Treasures

by Aaron Brazell on March 11, 2006 · 10 comments

I’ve been coding PHP for six years now and have gotten quite fluent and adept over the years. But every once in awhile, I’ll rediscover a long-lost gold mine of a function that is so perfect and so simple yet has been forgotten.

Enter array_push(). This function basically takes a value and pops it on the end of an existing array. For instance, a simplistic usage example is:

$animals = array('cat', 'dog', 'bird');
array_push($animals, 'lizard');
print_r($animals);
// displays
// Array [0] => cat [1] => dog [2] => bird [3] => lizard

Pretty basic, but imagine how useful it is when I want to do form validation on a bunch of submitted vales?

if(!validateField('a_username',$a_username))
    array_push($validation_errors, validateField('a_username',$a_username));
if(!validateField('a_password', $a_password))
    array_push($validation_errors, validateField('a_password', $a_password));

print_r($validation_errors);
// displays:
// Array [0] => Username already taken [1] => Password
//    length is too short. Must be between 6-16 characters

How did I forget about this function? It’s a Godsend…

{ 10 comments }

1

Dennis Pallett 03.11.06 at 3:25 pm

Or you could use the following:

$animals = array(’cat’, ‘dog’, ‘bird’);
$animals[] = ‘lizard’;
print_r($animals);
// displays
// Array [0] => cat [1] => dog [2] => bird [3] => lizard

which is much easier.

2

Dennis Pallett 03.11.06 at 3:25 pm

Or you could use the following:

$animals = array(’cat’, ‘dog’, ‘bird’);
$animals[] = ‘lizard’;
print_r($animals);
// displays
// Array [0] => cat [1] => dog [2] => bird [3] => lizard

which is much easier.

3

Dennis Pallett 03.11.06 at 3:25 pm

Or you could use the following:

$animals = array(’cat’, ‘dog’, ‘bird’);
$animals[] = ‘lizard’;
print_r($animals);
// displays
// Array [0] => cat [1] => dog [2] => bird [3] => lizard

which is much easier.

4

Dennis Pallett 03.11.06 at 3:25 pm

Or you could use the following:

$animals = array(’cat’, ‘dog’, ‘bird’);
$animals[] = ‘lizard’;
print_r($animals);
// displays
// Array [0] => cat [1] => dog [2] => bird [3] => lizard

which is much easier.

5

Dennis Pallett 03.11.06 at 3:25 pm

Or you could use the following:

$animals = array(’cat’, ‘dog’, ‘bird’);
$animals[] = ‘lizard’;
print_r($animals);
// displays
// Array [0] => cat [1] => dog [2] => bird [3] => lizard

which is much easier.

6

Aaron 03.11.06 at 3:39 pm

True, Dennis. :)

7

Aaron 03.11.06 at 3:39 pm

True, Dennis. :)

8

Aaron 03.11.06 at 3:39 pm

True, Dennis. :)

9

Aaron 03.11.06 at 3:39 pm

True, Dennis. :)

10

Aaron 03.11.06 at 3:39 pm

True, Dennis. :)

Comments on this entry are closed.

Older post: Fighter + Bird = Ejection and Crash

Newer post: WordPress Unattended Installs