Author Topic: PHP global variables  (Read 4599 times)

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
PHP global variables
« on: April 17, 2012, 12:05:10 AM »
I don't know if something has changed in newer versions of PHP, but global variables are not working anymore.

$x = 1;

function a() {
 global $x;
 echo $x; // prints nothing
}

Anyone know what might cause this?  I don't particularly like to do it this way, but that's the way the Paypal code is written and before I go and hack it to pieces, I want to know if there is a quick fix.

Steve

  • This 49%er supports Romney
  • Just a Jackass
  • *
  • Posts: 16120
  • Karma: +31/-410
  • Mr. Mom
Re: PHP global variables
« Reply #1 on: April 17, 2012, 12:13:16 AM »
wait.....aren't you echoing the local version of the variable in that code? That should return nothing, because you have not given it a value in that scope.

edit:

I'm ass ass. how the hell i didn't see the global x is beyond me. I need less vicatin
hey ethic if you and i were both courting lily allen..... oh wait, which one of us has a relationship that lasted more than the bus ride home?

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: PHP global variables
« Reply #2 on: April 17, 2012, 12:18:26 AM »
I don't know if something has changed in newer versions of PHP, but global variables are not working anymore.

$x = 1;

function a() {
 global $x;
 echo $x; // prints nothing
}

Anyone know what might cause this?  I don't particularly like to do it this way, but that's the way the Paypal code is written and before I go and hack it to pieces, I want to know if there is a quick fix.

Well you aren't calling a().  And what exact version of PHP.  For an error like this you'll have to give a working example.

Edit:  In the meantime make sure that the $x = 1; line is truly in the global scope.

Rob

  • New improved. Now with added something...
  • Jackass In Charge
  • Posts: 5959
  • Karma: +86/-149
  • Approaching 60 from the wrong damn direction...
Re: PHP global variables
« Reply #3 on: April 17, 2012, 03:31:20 AM »
I don't know if something has changed in newer versions of PHP, but global variables are not working anymore.

$x = 1;

function a() {
 global $x;
 echo $x; // prints nothing
}

Anyone know what might cause this?  I don't particularly like to do it this way, but that's the way the Paypal code is written and before I go and hack it to pieces, I want to know if there is a quick fix.

How about using the $GLOBAL superglobal

Code: [Select]
$x = 1;

function a() {
 $GLOBALS ['x'];
 echo $x; // prints nothing?
}
?


micah

  • A real person, on the Internet.
  • Ass Wipe
  • Posts: 6915
  • Karma: +58/-55
  • Truth cannot contradict truth.
    • micahj.com
Re: PHP global variables
« Reply #4 on: April 17, 2012, 08:29:26 AM »
are you using a framework?  PHP globals don't seem to work in most frameworks I've used (cake, CI, kohana)...
« Last Edit: April 17, 2012, 08:36:34 AM by micah »
"I possess a device, in my pocket, that is capable of accessing the entirety of information known to man.  I use it to look at pictures of cats and get in arguments with strangers."

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: PHP global variables
« Reply #5 on: April 17, 2012, 08:38:28 AM »
How about using the $GLOBAL superglobal

Code: [Select]
$x = 1;

function a() {
 $GLOBALS ['x'];
 echo $x; // prints nothing?
}
?

The GLOBALS superglobal is used to access global variables, not to bring them into the local scope.  So you'd have to do:

Code: [Select]
$x = 1;

function a() {
 echo $GLOBALS ['x']; // prints nothing?
}

Rob

  • New improved. Now with added something...
  • Jackass In Charge
  • Posts: 5959
  • Karma: +86/-149
  • Approaching 60 from the wrong damn direction...
Re: PHP global variables
« Reply #6 on: April 17, 2012, 08:45:09 AM »
How about using the $GLOBAL superglobal

Code: [Select]
$x = 1;

function a() {
 $GLOBALS ['x'];
 echo $x; // prints nothing?
}
?

The GLOBALS superglobal is used to access global variables, not to bring them into the local scope.  So you'd have to do:

Code: [Select]
$x = 1;

function a() {
 echo $GLOBALS ['x']; // prints nothing?
}



Ah, gotcha. That makes sense.

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: PHP global variables
« Reply #7 on: April 17, 2012, 10:29:47 AM »
I don't know if something has changed in newer versions of PHP, but global variables are not working anymore.

$x = 1;

function a() {
 global $x;
 echo $x; // prints nothing
}

Anyone know what might cause this?  I don't particularly like to do it this way, but that's the way the Paypal code is written and before I go and hack it to pieces, I want to know if there is a quick fix.

Well you aren't calling a().  And what exact version of PHP.  For an error like this you'll have to give a working example.

Edit:  In the meantime make sure that the $x = 1; line is truly in the global scope.
Sorry, that was a shitty example.  My actual example is from the Paypal API code.  They define a bunch of shit and then use global to access them inside of a few functions and the values aren't appearing inside of the functions.  I'm on PHP 5.3.8 with my host.

I'm not using a framework.  Other than the API files from Paypal, everything else is my own.

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: PHP global variables
« Reply #8 on: April 17, 2012, 10:33:04 AM »
The come up with the minimal example that shows the issue.  But I would guess that the values aren't being set while in the global scope.

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: PHP global variables
« Reply #9 on: April 17, 2012, 10:33:23 AM »
Here is some of the code:
Code: [Select]
require_once("constants.php");

if(defined('API_USERNAME'))
$API_UserName=API_USERNAME;

if(defined('API_PASSWORD'))
$API_Password=API_PASSWORD;

if(defined('API_SIGNATURE'))
$API_Signature=API_SIGNATURE;

if(defined('API_ENDPOINT'))
$API_Endpoint =API_ENDPOINT;

$version=VERSION;

if(defined('SUBJECT'))
$subject = SUBJECT;
// below three are needed if used permissioning
if(defined('AUTH_TOKEN'))
$AUTH_token= AUTH_TOKEN;

if(defined('AUTH_SIGNATURE'))
$AUTH_signature=AUTH_SIGNATURE;

if(defined('AUTH_TIMESTAMP'))
$AUTH_timestamp=AUTH_TIMESTAMP;


function nvpHeader()
{
global $API_Endpoint,$version,$API_UserName,$API_Password,$API_Signature,$nvp_Header, $subject, $AUTH_token,$AUTH_signature,$AUTH_timestamp;
$nvpHeaderStr = "";

If I echo any of the globals in the function, they're all blank even though they're defined outside of the function.

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: PHP global variables
« Reply #10 on: April 17, 2012, 10:36:17 AM »
After you set all those variables run this:
Code: [Select]
echo '<pre>'; print_r(get_defined_constants(true)); echo '</pre>';
And make sure those constants are actually defined.

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: PHP global variables
« Reply #11 on: April 17, 2012, 11:31:39 AM »
They're all defined.  What now?

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: PHP global variables
« Reply #12 on: April 17, 2012, 11:35:04 AM »
As a test
Code: [Select]
if(defined('API_USERNAME'))
$GLOBALS['API_UserName']=API_USERNAME;

If that doesn't work then those values are getting overwritten.   Oh and try this in the function:
Code: [Select]
echo '<pre>'; var_dump($API_Password); echo '</pre>';
I'm looking to see if the variable is null or an empty string (or something else).

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: PHP global variables
« Reply #13 on: April 17, 2012, 11:57:58 AM »
I did this outside for each:

global $API_Endpoint;
if(defined('API_ENDPOINT'))
$API_Endpoint =API_ENDPOINT;

And then just used:

$GLOBALS['API_ENDPOINT']

inside the function and that gives me access to it.  This is so fucked up but at least it's working now.

Now if I can figure out why it's letting the card process with an invalid CVV2 code...

Steve

  • This 49%er supports Romney
  • Just a Jackass
  • *
  • Posts: 16120
  • Karma: +31/-410
  • Mr. Mom
Re: PHP global variables
« Reply #14 on: April 17, 2012, 12:35:38 PM »
>>invalid cw2 code

thats not good
hey ethic if you and i were both courting lily allen..... oh wait, which one of us has a relationship that lasted more than the bus ride home?