You are here: Re: [PHP] array() « PHP « IT news, forums, messages
Re: [PHP] array()

Posted by Jochem Maas on 08/02/05 09:59

Edward Vermillion wrote:
> Sebastian wrote:
>
>> is it always necessary to call array() when you do something like this:
>>
>> mysql_query("SELECT ....");
>> while($rows .....)
>> {
>> $data[] = $rows;
>> }
>>
>> if so, why? i have a habit of never calling array() and someone told
>> me i shouldn't do this.
>>
>>
> If that's your first use of $data then it's not necessary, but it's very
> highly recommended to do something like:

DITTO! :-)

>
> $data = array();

the someone who told you don't is wrong - by setting $data to an array()
explicitly just before you use it you we be sure $data contains exactly what yuou think
it should when your while loop (in this case) completes...

imagine you came back to a script containing this code and you added a similiar
query loop above also using the var $data!

don't worry about the difference in performance (whether you initialize the var to
an array() or not) - you can't even measure it in any valid way.

>
> mysql_query("SELECT ....");
> while($rows .....)
> {
> $data[] = $rows;
> }
>

you might even consider that it is not always worth looping just to create
a $data array - often you can process/output/whatever each $row as you encounter it...

imagine what happens if your query returns a million records, your script could process
each $row 1 by 1 (granted it would take quite a while!) but trying to fill a $data array
would almost definitely cause your script to die due to memory exhaustion. (either a php limit
or a system limit)

> That way you _know_ that $data is "clean" before you start doing
> anything with it.
>
> It's always a good idea to set any variables you're using to some value,
> either "", array(), 0, or some default value, before you use them to
> help keep the "bad guys" out of your scripts.
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация