Re: Variable Scope

php.internals

Unnamed Person

23 years ago
Not so. I supplied this version earlier: int array[] = {1, 2, 3, 4, 5}; for (int i = 0; i < 5; i++) { int num = array[i]; printf("%d", num); for (int i = 0; i < 5; i++) printf("%d", i * num); } On Saturday, Aug 30, 2003, at 15:19 America/New_York, Rasmus Lerdorf wrote:

Ard Biesheuvel

23 years ago
> for (int i = 0; i < 5; i++) > { > int num = array[i]; > printf("%d", num); > for (int i = 0; i < 5; i++) > printf("%d", i * num); > }
I'll try to explain it in your own language. no declarations inside for() in C ===> your example ===> *crap* loose typing ===> no declarations ===> 'masking out' _IMPOSSIBLE_

Unnamed Person

23 years ago
NOTE: I am reentering the fray on provocation. On Saturday, Aug 30, 2003, at 19:59 America/New_York, Ard Biesheuvel wrote:
>> for (int i = 0; i < 5; i++) >> { >> int num = array[i]; >> printf("%d", num); >> for (int i = 0; i < 5; i++) >> printf("%d", i * num); >> }
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++) { int i; for (i = 5; i < 10; i++); } printf("%d", i);
> > I'll try to explain it in your own language. > > no declarations inside for() in C ===> your example ===> *crap* > > loose typing ===> no declarations ===> 'masking out' _IMPOSSIBLE_
How germane of you to adopt my own language, but as flattering as that is, I must say that your grammar is off a we bit. I would have chosen: no declarations inside for() in C =>i your example =========> *crap* loose typing =>i no declarations =========> 'masking out' _IMPOSSIBLE_

Ard Biesheuvel

23 years ago
> 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() ? 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

Unnamed Person

23 years ago
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.

DvDmanDT

23 years ago
As I've understood it your example is like only valid in the latest C++ standards or something... Possibly also in newer C... But it's new... MSVC6 does not have that implented I think... But I might be pretty badly misstaken on this one...
-- // DvDmanDT MSN: dvdmandt@hotmail.com Mail: dvdmandt@telia.com <LingWitt@insightbb.com> skrev i meddelandet news:E83F6ABB-DB1F-11D7-98BE-000393030CE6@insightbb.com...

Unnamed Person

23 years ago
I thank you for your open-mindedness, but I must admit I was in the fault. I supplied a version from a modern language such as C++. On Saturday, Aug 30, 2003, at 20:36 America/New_York, DvDmanDT wrote:

George Schlossnagle

23 years ago
On Saturday, August 30, 2003, at 09:12 PM, LingWitt@insightbb.com wrote:
> I thank you for your open-mindedness, but I must admit I was in the > fault. I supplied a version from a modern language such as C++.
No need to be a dick about it. Calling C++ 'modern' is about as sensible as calling C 'ancient'. Plenty of applications and operating systems are still written in C (not C++). Apache, PHP, Perl, Linux and FreeBSD are some examples. Rasmus responded correctly to your question by noting that PHP's for() syntax is identical to C's for() syntax. Which it is. PHP is not C. PHP is not C++. PHP is not Java. PHP is PHP. George

Unnamed Person

23 years ago
I was sincere. I meant "modern" to mean more recent. Though, I do agree with the correlation between your interpretation and response. I apologize. On Saturday, Aug 30, 2003, at 21:44 America/New_York, George Schlossnagle wrote: