Variable Scope

php.internals

Unnamed Person

23 years ago
Variable scope is mediocre at best. For instances: $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 inner loop $i doesn't mask the outer loop $i. Thus, the whole things gets screwed up.

Derick Rethans

23 years ago
On Sat, 30 Aug 2003 LingWitt@insightbb.com wrote:
> The inner loop $i doesn't mask the outer loop $i. > Thus, the whole things gets screwed up.
? This example works perfectly fine. What's your problem here? What do you mean with "mask the outer loop"? Of course if you modify $i INSIDE the loop the outer one is affected. Derick
-- "Interpreting what the GPL actually means is a job best left to those that read the future by examining animal entrails." ------------------------------------------------------------------------- Derick Rethans http://derickrethans.nl/ International PHP Magazine http://php-mag.net/ -------------------------------------------------------------------------

Unnamed Person

23 years ago
Since the inner loop declares $i again, it should mask the outer loop's $i as per other languages. Besides being useful, it makes more sense. Otherwise, the declaratory statement in the second loop is completely meaningless. On Saturday, Aug 30, 2003, at 10:21 America/New_York, Derick Rethans wrote:

Derick Rethans

23 years ago
On Sat, 30 Aug 2003 LingWitt@insightbb.com wrote:
> Since the inner loop declares $i again, it should mask the outer loop's > $i as per other languages. Besides being useful, it makes more sense.
No, that's not true. You don't redeclare ANYTHING here... you're just using the same variable $i, just like you would do in C.
> Otherwise, the declaratory statement in the second loop is completely > meaningless.
You don't declare anything... Derick
-- "Interpreting what the GPL actually means is a job best left to those that read the future by examining animal entrails." ------------------------------------------------------------------------- Derick Rethans http://derickrethans.nl/ International PHP Magazine http://php-mag.net/ -------------------------------------------------------------------------

Unnamed Person

23 years ago
On Saturday, Aug 30, 2003, at 10:33 America/New_York, Derick Rethans wrote:
> On Sat, 30 Aug 2003 LingWitt@insightbb.com wrote: > >> Since the inner loop declares $i again, it should mask the outer >> loop's >> $i as per other languages. Besides being useful, it makes more sense. > > No, that's not true. You don't redeclare ANYTHING here... you're just > using the same variable $i, just like you would do in C.
//In the following example, the inner loop 'i' masks the outer loop 'i': 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); }

Wez Furlong

23 years ago
PHP is not C++. Please go and carefully read the whole PHP manual from start to finish, and if you are still unsure of how to use PHP, ask your questions on the general list. Your posts have nothing to do with internals issues. --Wez.

Unnamed Person

23 years ago
This is not a how-to question. This is a statement. There was not question mark in any of my emails. This is criticism, and criticms=>improvement=========>development. On Saturday, Aug 30, 2003, at 10:54 America/New_York, Wez Furlong wrote:

Rasmus Lerdorf

23 years ago
PHP, is a loosely typed language. If you want a strongly typed language where you can redeclare and re-use the same variable names within the same scope, then use a strongly typed language. There are plenty of them around. This will never ever change in PHP. -Rasmus On Sat, 30 Aug 2003 LingWitt@insightbb.com wrote:

Derick Rethans

23 years ago
On Sat, 30 Aug 2003 LingWitt@insightbb.com wrote:
> > On Saturday, Aug 30, 2003, at 10:33 America/New_York, Derick Rethans > wrote: > > > On Sat, 30 Aug 2003 LingWitt@insightbb.com wrote: > > > >> Since the inner loop declares $i again, it should mask the outer > >> loop's > >> $i as per other languages. Besides being useful, it makes more sense. > > > > No, that's not true. You don't redeclare ANYTHING here... you're just > > using the same variable $i, just like you would do in C. > > //In the following example, the inner loop 'i' masks the outer loop 'i': > > 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); > }
*SIGH* THIS EXAMPLE IS NOT THE SAME AS THE ONE IN PHP. IN PHP YOU DON'T DECLARE VARIABLES, THIS IS DONE AUTOMATICALLY FOR YOU THE FIRST TIME YOU USE IT. THEREFORE THE $i IN THE SECOND LOOP IS THE SAME VARIABLE AS IN THE FIRST LOOP. AGAIN, TAKES THIS CRAP OF THIS LIST AS IT IS NOT, I REPEAT *NOT* AN INTERNALS PROBLEM. DERICK
-- "Interpreting what the GPL actually means is a job best left to those that read the future by examining animal entrails." ------------------------------------------------------------------------- Derick Rethans http://derickrethans.nl/ International PHP Magazine http://php-mag.net/ -------------------------------------------------------------------------