Page 1 of 1

array identifying

Posted: Wed Mar 23, 2005 2:06 am
by mudkicker
i have a question.

for example in a loop we use an array, we add some info in this array.

Code: Select all

<?php
// just an example
for($i=0; $i<10000; $i++)
{
$array[] = $data[$i];
}
?>
Now, is it better/faster to identify that $array is an array before we put it in that loop?

Code: Select all

<?php
// just another example
$array = array();
for($i=0; $i<10000; $i++)
{
$array[] = $data[$i];
}
?>
I tried to make a test but first results were faster than identifying or am I mistaken?
It was a surprise for me too because I used alwas the second one.

Thanks,

Posted: Wed Mar 23, 2005 3:11 am
by CoderGoblin
If you use :

Code: Select all

error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
you will get a warning that $array is not set if you do not define it first.

As a programmer I find it is always useful to declare variables with a default value before use (I think of this as a good programming style). One advantage of this is that other people can see what the variable is expected to contain.

Timing wise there should not be much/if any of a difference.

Posted: Wed Mar 23, 2005 3:54 am
by mudkicker
Thanks for your opinions. I was thinking like you, too but I was confused. :) Well, not anymore ;) Thanks!