43
In php I can do this without declaring/defining variables:
echo($foo); //resolve: vazio, sem nada
$foo++;
echo($foo); // resolve: 1
Using var_dump()
the same gives, respectively NULL
and 1
.
If I use settype($foo, "integer");
before , the same gives 0
and 1
.
The point is, is there is some performance, use or other implication that justifies avoiding these uses:
//usar um contador sem o defenir ou resetar primeiro
$foo++;
// adicionar elementos a uma array
// sem os defenir $bar = array('2010'=>0,'2011'=>0); primeiro
$bar['2010']++;
$bar['2010']++;
$bar['2011']++; // ou $bar['2011'] = 'blah';
// resolve: array(2) { [2010]=> int(2) [2011]=> int(1) }
As poetic as it is, I agree that the issue has made the question clearer. I will not answer the question because it is not what you expect, but you should always do what is most readable, which better indicates the intention, and declare variables, even if it is not necessary, is a way of legibility and containment of mysterious bugs. Performance should be the last concern, in general almost never when it comes to PHP.
– Maniero
For an update of this discussion: The variables were not officially regulated in any PSR, which in my opinion should be in [PSR-1](http://www.php-fig.org/psr/psr-1 "Basic Coding") and [PSR-2](http://www.php-fig.org/psr/psr-2 "Coding Style") Therefore, good practices / performance / usage, goes from the programmer to my view. I liked all the answers given here, but, everything really depends on the need of the program.
– William Aparecido Brandino