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 }
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.
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.
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.
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.
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.
Aaron 03.11.06 at 3:39 pm
True, Dennis. :)
Aaron 03.11.06 at 3:39 pm
True, Dennis. :)
Aaron 03.11.06 at 3:39 pm
True, Dennis. :)
Aaron 03.11.06 at 3:39 pm
True, Dennis. :)
Aaron 03.11.06 at 3:39 pm
True, Dennis. :)
Comments on this entry are closed.