Click to See Complete Forum and Search --> : PHP question.


FatalFanatic
August 19th, 2003, 11:21 AM
There are 2 very similar PHP scripts. The first one works properly, the second one does not.
If this works:

<?php
$x[0] = 1;
$y = 1;
function YaY($a,$b)
{
print $a[0];
print $b;
}
YaY($x,$y);
?>
Then why does this not: :(

<?php
$x[0] = 1;
$y = 1;
function YaY($a,$b)
{
global $a, $b;
print $a[0];
print $b;
}
YaY($x,$y);
?>
And how could I make it work with the same effect?
Thanks in advance.

jdenzer
August 23rd, 2003, 03:42 AM
by declaring
global $a, $b;
$a and $b can be used globally(outside the function)
and the are not initiallized with a value
Either change the $a $b to a different var name
of initialize it to something.




<?php
$x[0] = 1;
$y = 1;
function YaY($a,$b)
{
global $a, $b; //Either change var names

//Or give them a value
$a[0] = 22;
$b =33;

print "inside func $a[0] <br>";
print "inside func $b <br><br>";
}
YaY($x,$y);

print "Outside func $a[0] <br>";
print "Outsidefunc $b <br><br>";
?>