Jump to content

Problem with global variables in function


Warz

Recommended Posts

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?  :confused:

 

Old server: PHP Version 5.2.9

New server: PHP Version 5.1.6

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.