timjahn Posted May 15, 2013 Share Posted May 15, 2013 Hey:) I have a question. It may sound dumb, but I can't figure out the problem. What I have: <html><body> <?php $x=5; $z=6; $y=$x+$z; function myTest() { global $x, $z, $y; echo $x . " " . $z . " " . $y . " "; $x++; $z++; } myTest(); myTest(); echo "<br>"; echo $x . " " . $z . " " . $y; ?> </body> </html> Now my problem is that I don't know why the value of y doesn't change. x and z are changing fine, but y isn't. Thanks:) Tim Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 15, 2013 Share Posted May 15, 2013 (edited) What are you getting output and what are you expecting to be output? EDIT: I ran that code and what I received was Output from first call to myTest(): X = 5, Z = 6, Y = 11Output from first call to myTest(): X = 6, Z = 7, Y = 11Output after second call to myTest(): X = 7, Z = 8, Y = 11 Which is correct. No where in that code is the value of $y being modified. Edited May 15, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted May 15, 2013 Solution Share Posted May 15, 2013 If you say "$y=$x+$z" you are not defining a math equation but are doing an assignment: take $x, add $z, and save that to $y. If $x or $z changes that won't affect $y because you're changing them after you did the addition and assignment. Quote Link to comment Share on other sites More sharing options...
timjahn Posted May 15, 2013 Author Share Posted May 15, 2013 If you say "$y=$x+$z" you are not defining a math equation but are doing an assignment: take $x, add $z, and save that to $y. If $x or $z changes that won't affect $y because you're changing them after you did the addition and assignment. I changed it to that..: <html> <body> <?php $x=5; $z=6; $y=$x+$z; function myTest() { global $x, $z, $y; echo $x . " " . $z . " " . $y . " "; $x++; $z++; $y=$x+$z; } myTest(); myTest(); echo "<br>"; echo $x . " " . $z . " " . $y; ?> </body> </html> ..and it is working:) thank you so much;) Thanks to you too, psycho Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 16, 2013 Share Posted May 16, 2013 Now forget that global exists and learnthe right way. Quote Link to comment Share on other sites More sharing options...
timjahn Posted May 16, 2013 Author Share Posted May 16, 2013 Now forget that global exists and learnthe right way. ... not exactly helpful.. How? Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 16, 2013 Share Posted May 16, 2013 Go read the manual on user defined functions. Quote Link to comment Share on other sites More sharing options...
timjahn Posted May 16, 2013 Author Share Posted May 16, 2013 Go read the manual on user defined functions. I will do that, thanks:)! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.