array identifying

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

array identifying

Post 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,
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

Thanks for your opinions. I was thinking like you, too but I was confused. :) Well, not anymore ;) Thanks!
Post Reply