0

Global variable can't be read on my function. Anyone know what's wrong with my code below? Please help me.

<?php
global $a = array(2,3,4); 
global $b = array(3,5,6); 

function test(){
        $y = $a[0]*$b[0];
        return $y;
}
test();
?>
Matt Harrison
  • 12,524
  • 6
  • 41
  • 63

4 Answers4

1
<?php
$a = array(2,3,4); 
$b = array(3,5,6); 

function test(){
GLOBAL $a;
GLOBAL $b;
        $y = $a[0]*$b[0];
        return $y;
}
test();
?>
Manish Jangir
  • 519
  • 3
  • 9
  • You mean If I have a lot of function on my code, So I've to rewrite the variable in every function as global variable? – Victoria Cho May 18 '13 at 04:40
  • yes, to access a global variable from within a function, you need to use the global keyword every time – Potheek May 18 '13 at 04:45
  • This answer needs one thing changed. At the bottom test(); is invoked. But the return value goes nowhere and the function itself has no echo statements. Therefore, it will show nothing to the user even though the function is working. – Ryan Mortensen May 18 '13 at 04:51
  • Unipartisandev is right. You have to assign the return value of function in a variable. it should be $result = test(); – Manish Jangir May 18 '13 at 08:01
1

It seems you have some misconception regarding the global variables.

You don't declare the variables as global ..instead you tell php that you want to use the use the variable that is not in scope of function

So your code will be

<?php
  $a = array(2,3,4); 
  $b = array(3,5,6); 

function test()
{
    global $a, $b;
    $y = $a[0]*$b[0];
    // or you can use $GLOBAL['a'][0] * $GLOBAL['b'][0]
    return $y;
 }
echo test();
 ?>

Now with this code you are saying that use variables $a and $b that are defined outside the scope of function.

DOCUMENTATION

Hope it helps you and you understand what I want to say

alwaysLearn
  • 6,426
  • 6
  • 33
  • 63
0

First you have to declare the global.

global $a;

Then you can make it an array.

$a = array(2,3,4);

Also, if you're using a function. Declare global inside the function, not outside of it.

<?php
$a = array(2,3,4); 
$b = array(3,5,6); 

$output = test($a,$b);
echo $output;

function test($array1,$array2)
{
$y = $array1[0]*$array2[0];
return $y;
}
?>

Here's an explanation for you. Where we have:

function test($array1,$array2)

We are saying that any value that is put in those spots when the function is called is treated as $array1 and $array2 INSIDE the function.

So when we call it we say test($a,$b) So when the function runs $array1 == $a, and $array2 == $b.

Inside the function the variables $a and $b basically becomes $array1 and 2.

The return value makes the function call basically be the equivalent of that return outside of the function so that:

$output = test($a,$b);

Is exactly like saying:

$output = 6;

I hope that helps.

Ryan Mortensen
  • 2,197
  • 2
  • 20
  • 26
  • I see at the bottom you put test(); all by itself. You need to tell the program what to do with the return value. For example, write $output = test(); echo $output; If you only put test(); then the function runs, and that's it. It knows the answer, but doesn't show it to you. – Ryan Mortensen May 18 '13 at 04:45
  • Whoa~ You help me a lot. Thank you :) – Victoria Cho May 18 '13 at 05:15
  • So in this case, if we call test() and put test(5,30); it would return 150. It doesn't HAVE TO be a $variable name with a $ in the front. – Ryan Mortensen May 18 '13 at 05:29
0

Use the global keyword to include a variable in scope.

Here's how it would look with the snippet you provided.

$a = array(2, 3, 4); 
$b = array(3, 5, 6); 

function test() {
    // include $a and $b in the scope of this function.
    global $a, $b;

    $y = $a[0] * $b[0];

    return $y;
}

Visit the documentation link provided above to see more syntax of the global keyword.

You can additionally use the $GLOBALS array to access a and b.

$y = $GLOBALS['a'][0] * $GLOBALS['b'][0];
Austin Brunkhorst
  • 18,784
  • 5
  • 40
  • 56
  • You mean If I have a lot of function on my code, So I've to rewrite the variable in every function as global variable? – Victoria Cho May 18 '13 at 04:45
  • 1
    You do not have to do this. That is what parameters are for. You can pass `$a` and `$b` as arguments to the function rather than making them global. It would essentially be function `test($a, $b) {}` and the call to test would be `test($a, $b);` – Austin Brunkhorst May 18 '13 at 04:47
  • Austin is exactly right. If you are trying to pass a value or values to the function that is the way to do it. Usually globals are used when you need the values the function creates somewhere outside of the function and there is more than one variable that would be too inconvenient or impractical to put into an array for a return value. A function can only send one return value, but it may be an array. – Ryan Mortensen May 18 '13 at 04:54
  • I see.. cz $a and $b are arrays variable , so it can't set as arguments test($a, $b) {}. Thank you :) – Victoria Cho May 18 '13 at 05:05
  • 1
    That's not true. They can be. I know I am sounding like a broken record, but have you added an echo statement? I am starting to think that's really what the problem is. – Ryan Mortensen May 18 '13 at 05:10
  • The problem @VictoriaCho is that you may need to brush up on the fundamentals of PHP or better yet programming. – Austin Brunkhorst May 18 '13 at 05:14
  • Ya, I just realized that echo is one of my prob too >< I'm still newbie on programming. I have to learn many more about it I guess.. :) Anw, Thank you so much, you help me a lot :) – Victoria Cho May 18 '13 at 05:25