[PHP] scope problem
Apparently I'm having some kind of meltdown here. Can anyone explain the logic behind why the following variable has the original value and how I can pull/push the value to access it at the end? while loop { $variable = 100; while loop { switch($othervar) { case 1: $variable = $variable + 100; break; case 2: $variable = $variable + 200; break; case 3: $variable = $variable + 300; break; } echo $variable.br; } echo brThe final value is .$variable.br; } This gives values something to the tune of... 200 400 700 100 I usually have variables set outside of a while loop that increment based on the contents of the loop and I could swear that they hold the value on the other side of the loop. I don't usually use break; in my scripts unless I'm using switch. However, I would think that if using break was throwing me, that the value wouldn't print on each cycle of the loop. TIA Larry -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] scope problem
Hello Larry, Friday, March 5, 2004, 4:01:39 PM, you wrote: LB This gives values something to the tune of... LB 200 LB 400 LB 700 LB 100 Hard to say with so little code, but... Your first where loop is probably running twice, i.e. resetting variable back to 100 after the 2nd (internal) where loop has finished modifying it. Move $variable = 100 above the first where loop and see what happens. -- Best regards, Richard Davey http://www.phpcommunity.org/wiki/296.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] scope problem
Thanks for the help, but it turned out to be the nut on top of the keyboard! Something I missed. Good to know my concept of scope with php is still healthy (at least). Thanks -Original Message- From: Richard Davey [mailto:[EMAIL PROTECTED] Sent: Friday, March 05, 2004 11:17 AM To: PHP List Subject: Re: [PHP] scope problem Hello Larry, Friday, March 5, 2004, 4:01:39 PM, you wrote: LB This gives values something to the tune of... LB 200 LB 400 LB 700 LB 100 Hard to say with so little code, but... Your first where loop is probably running twice, i.e. resetting variable back to 100 after the 2nd (internal) where loop has finished modifying it. Move $variable = 100 above the first where loop and see what happens. -- Best regards, Richard Davey http://www.phpcommunity.org/wiki/296.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Scope problem in while loop
According to the PHP 4 docs all variables are global unless within a function. I've got the following test code which should output 2, but outputs 1. The while loop creates it's own class object (which seems strange since it isn't explicitly instantiated by my code; I would think it would cause errors). The reason I created an object for my variable (actually, I started testing with regular strings) is that I've had similar scoping problems in perl, where variables got out of scope within loops (or even if statements). In perl, declaring an object outside these structures will protect it's scope. Not so in PHP I see. Can anyone explain this behavior? Do I have to create functions that return values every time I need a loop that modifies a variable? ?php class Ccust_data { function Cform_data() { $this-test = ; } } $o = new Ccust_data(); $o-test = 1; while ($y = 0) { global $o-test; $o-test = 2; $y = 1; } echo \$o-test = $o-test\n; ? -- Randy Perry sysTame Mac Consulting/Sales phn 561.589.6449 mobile email[EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Scope problem in while loop
On Mon, 2002-03-11 at 18:51, Randall Perry wrote: According to the PHP 4 docs all variables are global unless within a function. I've got the following test code which should output 2, but outputs 1. The while loop creates it's own class object (which seems strange since it isn't explicitly instantiated by my code; I would think it would cause errors). Actually, on PHP 4.2.0-dev, I get a parse error on the 'global $o-test;' line (you can't globalize an object attribute). If I remove that line, the code does indeed produce '$o-test = 1;'. That's because the loop never executes. Consider this change: while ($y = 0) { echo In the loop.\n; $o-test = 2; $y = 1; } When executed, 'In the loop' is never printed. This is because the loop never executes--because the while() condition is wrong. The '=' should be a '==' or '==='. What's happening at the moment is that the while() condition '$y = 0' is assigning 0 to $y, not comparing 0 to $y. The overall value of that expression, then, is 0--which evaluates to false. So the while loop never runs. If I change the '=' to '==', I get the correct output: Notice: Undefined variable: y in /home/torben/public_html/phptest/__phplist.html on line 27 In the loop. $o-test = 2 The notice is easily corrected by initializing $y before testing its value. The following should work for you: ?php error_reporting(E_ALL); class Ccust_data { function Cform_data() { $this-test = ; } } $o = new Ccust_data(); $o-test = 1; $y = 0; while ($y == 0) { echo In the loop.\n; $o-test = 2; $y = 1; } echo \$o-test = $o-test\n; ? What does this code do for you? Torben The reason I created an object for my variable (actually, I started testing with regular strings) is that I've had similar scoping problems in perl, where variables got out of scope within loops (or even if statements). In perl, declaring an object outside these structures will protect it's scope. Not so in PHP I see. Can anyone explain this behavior? Do I have to create functions that return values every time I need a loop that modifies a variable? ?php class Ccust_data { function Cform_data() { $this-test = ; } } $o = new Ccust_data(); $o-test = 1; while ($y = 0) { global $o-test; $o-test = 2; $y = 1; } echo \$o-test = $o-test\n; ? -- Randy Perry sysTame Mac Consulting/Sales -- Torben Wilson [EMAIL PROTECTED] http://www.thebuttlesschaps.com http://www.hybrid17.com http://www.inflatableeye.com +1.604.709.0506 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Scope problem in while loop
I may be wrong, but that's exactly what I ended up having to do... but don't quote me - I'm just learning OOP http://www.apokalyptik.com/forum/viewtopic.php?topic=140forum=60 -Original Message- From: Randall Perry [mailto:[EMAIL PROTECTED]] Sent: Monday, March 11, 2002 6:52 PM To: [EMAIL PROTECTED] Subject: [PHP] Scope problem in while loop According to the PHP 4 docs all variables are global unless within a function. I've got the following test code which should output 2, but outputs 1. The while loop creates it's own class object (which seems strange since it isn't explicitly instantiated by my code; I would think it would cause errors). The reason I created an object for my variable (actually, I started testing with regular strings) is that I've had similar scoping problems in perl, where variables got out of scope within loops (or even if statements). In perl, declaring an object outside these structures will protect it's scope. Not so in PHP I see. Can anyone explain this behavior? Do I have to create functions that return values every time I need a loop that modifies a variable? ?php class Ccust_data { function Cform_data() { $this-test = ; } } $o = new Ccust_data(); $o-test = 1; while ($y = 0) { global $o-test; $o-test = 2; $y = 1; } echo \$o-test = $o-test\n; ? -- Randy Perry sysTame Mac Consulting/Sales phn 561.589.6449 mobile email[EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Scope problem in while loop
Whoops, you're right. Classic 'C' mistake using = instead of ==. Never mind :( On Mon, 2002-03-11 at 18:51, Randall Perry wrote: According to the PHP 4 docs all variables are global unless within a function. I've got the following test code which should output 2, but outputs 1. The while loop creates it's own class object (which seems strange since it isn't explicitly instantiated by my code; I would think it would cause errors). Actually, on PHP 4.2.0-dev, I get a parse error on the 'global $o-test;' line (you can't globalize an object attribute). If I remove that line, the code does indeed produce '$o-test = 1;'. That's because the loop never executes. Consider this change: while ($y = 0) { echo In the loop.\n; $o-test = 2; $y = 1; } When executed, 'In the loop' is never printed. This is because the loop never executes--because the while() condition is wrong. The '=' should be a '==' or '==='. What's happening at the moment is that the while() condition '$y = 0' is assigning 0 to $y, not comparing 0 to $y. The overall value of that expression, then, is 0--which evaluates to false. So the while loop never runs. If I change the '=' to '==', I get the correct output: Notice: Undefined variable: y in /home/torben/public_html/phptest/__phplist.html on line 27 In the loop. $o-test = 2 The notice is easily corrected by initializing $y before testing its value. The following should work for you: ?php error_reporting(E_ALL); class Ccust_data { function Cform_data() { $this-test = ; } } $o = new Ccust_data(); $o-test = 1; $y = 0; while ($y == 0) { echo In the loop.\n; $o-test = 2; $y = 1; } echo \$o-test = $o-test\n; ? What does this code do for you? Torben The reason I created an object for my variable (actually, I started testing with regular strings) is that I've had similar scoping problems in perl, where variables got out of scope within loops (or even if statements). In perl, declaring an object outside these structures will protect it's scope. Not so in PHP I see. Can anyone explain this behavior? Do I have to create functions that return values every time I need a loop that modifies a variable? ?php class Ccust_data { function Cform_data() { $this-test = ; } } $o = new Ccust_data(); $o-test = 1; while ($y = 0) { global $o-test; $o-test = 2; $y = 1; } echo \$o-test = $o-test\n; ? -- Randy Perry sysTame Mac Consulting/Sales -- Randy Perry sysTame Mac Consulting/Sales phn 561.589.6449 mobile email[EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php