Warz Posted October 16, 2009 Share Posted October 16, 2009 Hi, After I changed server, php global variable in functions didn't work anymore!? See this code: $a = 1; $b = 2; function Sum() { global $a, $b; $sum = $a + $b; return $sum; } $sum = Sum(); echo "Result is: ".$sum; Now I get: "Result is: 0" On my old server I got "Result is: 3"... Anyone know what this could be? Old server: PHP Version 5.2.9 New server: PHP Version 5.1.6 Link to comment https://forums.phpfreaks.com/topic/177976-problem-with-global-variables-in-function/ Share on other sites More sharing options...
simshaun Posted October 16, 2009 Share Posted October 16, 2009 Works for me. Check your server installation. Link to comment https://forums.phpfreaks.com/topic/177976-problem-with-global-variables-in-function/#findComment-938391 Share on other sites More sharing options...
Warz Posted October 16, 2009 Author Share Posted October 16, 2009 I know, it works for me too on my old server, but what can I check for? I have no idea Link to comment https://forums.phpfreaks.com/topic/177976-problem-with-global-variables-in-function/#findComment-938402 Share on other sites More sharing options...
simshaun Posted October 16, 2009 Share Posted October 16, 2009 Are you using that code by itself in a file or are there other lines in the file? Link to comment https://forums.phpfreaks.com/topic/177976-problem-with-global-variables-in-function/#findComment-938408 Share on other sites More sharing options...
simshaun Posted October 16, 2009 Share Posted October 16, 2009 Well, I'm heading home for the weekend. 1. run a phpinfo(), save it. 2. revert back to the previous php version temporarily. 3. run a phpinfo(), save it. 4. compare the two and see whats different. Link to comment https://forums.phpfreaks.com/topic/177976-problem-with-global-variables-in-function/#findComment-938412 Share on other sites More sharing options...
Warz Posted October 16, 2009 Author Share Posted October 16, 2009 That's just an example code I tested on both servers... my real code is different obviously and has a lot more lines, however I tried this exact code on both servers, and it's just different behaviour/result. Link to comment https://forums.phpfreaks.com/topic/177976-problem-with-global-variables-in-function/#findComment-938413 Share on other sites More sharing options...
PFMaBiSmAd Posted October 16, 2009 Share Posted October 16, 2009 Your incoming variables probably don't have any value in them, have you checked? And you probably have a register_globals problem. Either you have some way out of date code that is relying on register_globals to magically populate variables and register_globals are off or you have some same name post/get/cookie/session/general variables with the same name and register_globals are on and the variables are overwriting each other. Could also be a short open tag problem and part of your code is not being parsed as php code. It would taking seeing your actual code in order to tell you exactly why it is not working. Link to comment https://forums.phpfreaks.com/topic/177976-problem-with-global-variables-in-function/#findComment-938421 Share on other sites More sharing options...
Warz Posted October 17, 2009 Author Share Posted October 17, 2009 Your incoming variables probably don't have any value in them, have you checked?Yes, they both have values, however global $a; doesn't seem to get any values.... when I go "echo $a;" inside the function it echos out nothing, however outside the function it works... so the problem has to do with the fact that "global" some how doesn't work. And you probably have a register_globals problem. Either you have some way out of date code that is relying on register_globals to magically populate variables and register_globals are off or you have some same name post/get/cookie/session/general variables with the same name and register_globals are on and the variables are overwriting each other. Could also be a short open tag problem and part of your code is not being parsed as php code. It would taking seeing your actual code in order to tell you exactly why it is not working. Well register globals are off. I'm not sure which setting would magically populate my variables tbh. And one more thing, this might not be a server issue after all. I just tried creating a 100% blank php file and filled it with this script and it worked... So then obviously it must be something that my current script is causing... I havn't made that script myself and it's pretty huge. To make sure there isn't any conflicts between variables I named them something completely random like "hadwawiodhawo" and "hadoakwdjowjwo" and still didn't work. Now it must be said that I'm including this code that doesn't work correctly on a template page, maybe there are some issues here. I might just have to do it the hard way but not using globals :'( Link to comment https://forums.phpfreaks.com/topic/177976-problem-with-global-variables-in-function/#findComment-938428 Share on other sites More sharing options...
PFMaBiSmAd Posted October 17, 2009 Share Posted October 17, 2009 If you have a large script that was created using poor programming like using the global keyword to being values into functions, good luck finding the problem. The use of the global keyword is an indication that the rest of the code is probably convoluted and it will be difficult to find and then to fix the problem. This is exactly why the global keyword should never be used. One of the points of using functions are they can make organizing larger programs easier, not harder. Link to comment https://forums.phpfreaks.com/topic/177976-problem-with-global-variables-in-function/#findComment-938477 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.