Re: Declaring arrays? Good practice?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux