On Sunday, Aug 31, 2003, at 06:16 America/New_York, Ard Biesheuvel wrote:

Perhaps this is an example from C++, but it illustrates my point. This may be a better example, if we are to step back a decade or two:
int i;
for (i = 0; i < 5; i++)

What is so 'inherently declaratory' about this for() ?

Nothing; I wish to concede: If you look further down the example, you will see a declaration in the first for loop block. This masks the i. My experience has always been that the declaration of control variables in a for loop occur in the first part. This is obviously not the case for strict C or anything base off of it, such as PHP. Yet, in the aforementioned example, I was illustrating that even in C, blocks delimit scope, and in my emails I was saying that the first part of the for loop, which is unnecessary if not used for declarations, should perhaps be considered declaratory in nature. However, considering that PHP has one scope, as mentioned elsewhere --that is, the function scope-- it would indeed be inconsistent and specialized. I thank you for your time, and hope to work with you later.



With this example, you've illustrated the exact reason why this is not possible in PHP. The first part of a for() is for declaration _and_ initialization. Without mandatory declaration (like in PHP), the compiler will not be able to distinguish the two cases.


In terms of your own example:

> $array = array(1, 2, 3, 4, 5);
>
> for ($i = 0; $i < 5; $i++)
> {
>     $num = $array[$i];
>     echo $num;
>     for ($i = 0; $i < 5; $i++)
>     {
>         echo $num * $i;
>     }
> }

'The whole thing gets screwed up' is only defined in terms of what you expect the result to be. How is the compiler to know that you're asking for a new variable in the inner for() loop ? It doesn't crash, does it ?

As the number of possible variable names in PHP is sufficient for most applications, using the same variable name for different variables is just /bad/ coding style.

Ard

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php


-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to