You are here: Re: [PHP] Re: Declaring arrays? Good practice? « PHP « IT news, forums, messages
Re: [PHP] Re: Declaring arrays? Good practice?

Posted by Jochem Maas on 09/28/13 11:34

Al wrote:
> However....
>
> For loops:
>
> $new_array= array();
>
> foreach($old array as $value){
>
> $new_array= $value:
> }
>
> Otherwise, all you'll get it the last assignment of $new_array as a
> variable, not an array.

which is exactly what you get if yuou run the code above - after
the foreach loop $new_array will be set to the value of the
last item of $old_array.

regardless of whether you init the var you still have to
use the square-bracket notation to add items to the array.

>
> You can also use;
>
> foreach($old array as $value){
>
> $new_array[]= $value:
> }
>
> But sometimes, if the expression is a bit fancy in the loop and you are
> using $keys, it is easy to make a mistake.
>
> Al..........
>
> Michael Hulse wrote:
>
>> Sorry if this question sounds noobish:
>>
>> $foo = array(); // Declare first, good practice.
>> $foo = array('one', 'two', 'three');
>>
>> Using above as example, is it good to always declare the array first?
>>
>> I usually/always declare my arrays first, but sometimes I get a bit
>> confused... for example:
>>
>> function give_foo() {
>> $the_foo = array(); // Declare first, good practice.
>> $the_foo = array('one', 'two', 'three');
>> // Do some stuff here...
>> return $the_foo;
>> }
>> $foo = give_foo(); // Declare $foo also?
>>
>> So, should I declare $foo as array and then give it the value of the
>> function? Example:
>>
>> $foo = array(); // Declare first, good practice.
>> $foo = give_foo();
>>
>> That seems kinda redundant. Thoughts?
>>
>> Sorry if this is a stupid question... I am self-taught, and I am
>> trying to shake all my bad coding habits.
>
>

 

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

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