PHP Treasures
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
1 | <a href="http://www.php.net/array_push/">array_push()</a> |
. This function basically takes a value and pops it on the end of an existing array. For instance, a simplistic usage example is:
1
2
3
4
5 $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?
1
2
3
4
5
6
7
8
9 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…
Pick up your copy of the WordPress Bible, a wildly popular resource for beginners and experts alike.
Popularity: 1% [?]



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.
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.
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.
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.
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.
True, Dennis. :)
True, Dennis. :)
True, Dennis. :)
True, Dennis. :)
True, Dennis. :)