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

Posted by Robert Cummings on 10/19/49 11:34

On Tue, 2005-12-13 at 22:34, Michael Hulse wrote:
> Sorry if this question sounds noobish:
>
> $foo = array(); // Declare first, good practice.

The above is a declaration.

> $foo = array('one', 'two', 'three');

The above is ALSO a declaration.

The first declaration is redundant and unnecessary and suggests you
don't fully grasp declarations :)

> 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.

Again a nice declaration above :)

> $foo = give_foo();

Explicit assignment thus making the prior declaration redundant;
however, if you are the kind of person who generally declares all of
your variables at the top of a function, and the assignment to the
return value of give_foo() occurs later in the code, then I would say it
is good practice. But if you do the declaration immediately before in
arbitrary places in the code, then I'd say poor practice since nothing
is gained by the forward declaration.

> 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.

No need to apologize. Sounds like you're learning to fish.

Cheers,
Rob.
--
..------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'

 

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

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