Technosailor.com Readers! Donate today to assist the Partners in Health Haiti Relief in their efforts.

11 March 2006 10 Comments

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% [?]

Tags: ,

10 Responses to “PHP Treasures”

  1. Dennis Pallett 11 March 2006 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 11 March 2006 at 3:25 pm #

    Or you could use the following:

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

    which is much easier.

  3. Dennis Pallett 11 March 2006 at 3:25 pm #

    Or you could use the following:

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

    which is much easier.

  4. Dennis Pallett 11 March 2006 at 3:25 pm #

    Or you could use the following:

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

    which is much easier.

  5. Dennis Pallett 11 March 2006 at 3:25 pm #

    Or you could use the following:

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

    which is much easier.

  6. Aaron 11 March 2006 at 3:39 pm #

    True, Dennis. :)

  7. Aaron 11 March 2006 at 3:39 pm #

    True, Dennis. :)

  8. Aaron 11 March 2006 at 3:39 pm #

    True, Dennis. :)

  9. Aaron 11 March 2006 at 3:39 pm #

    True, Dennis. :)

  10. Aaron 11 March 2006 at 3:39 pm #

    True, Dennis. :)