PDA

View Full Version : PHP 5.2.4 in Ubuntu 8.04 "global" broken.



TitanKing
October 9th, 2008, 09:56 AM
Very odd problem, I realized something odd was going on when I needed to create a simple function with global variables inside function, according to the manual, this should print the number 3;


$a = 1;
$b = 2;

function Sum()
{
global $a, $b;

$b = $a + $b;
}

Sum();
echo $b;

However, mine prints the original $b which is 2, it seems it is ignoring global.

Is this a bug?

ianhaycox
October 9th, 2008, 11:50 AM
For your example my system, PHP Version 5.2.4-2ubuntu5.3, on Hardy prints 3 !

TitanKing
October 9th, 2008, 12:08 PM
Thanks for your reply. This is very weird, well I will just have to work around this problem as I have no idea what could be causing this.

Regards

rnodal
October 9th, 2008, 09:02 PM
Could you give it a try to this as see if you get the same result?

<?php
$a = 1;
$b = 2;

function Sum()
{
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}

Sum();
echo $b;
?>


-r