-1

I am new to php so I do not know much about $_GLOBALS. I expect these are used to access global variables anywhere. So, my question is what is the problem inside function addition(), why I cannot do this? Can't I directly assign a value to the variable z.

    <?php 
    $x = 75; 
    $y = 25;

    function addition () { 
        $z = $GLOBALS['x'] + $GLOBALS['y']; 
    }

    addition(); 
    echo $z; 
    ?>

I get following error:

Notice: Undefined variable: z in C:\xampp\htdocs\php\first.php on line 14

which is echo $z;

Robert
  • 19,800
  • 5
  • 55
  • 85
StealthTrails
  • 2,281
  • 8
  • 43
  • 67
  • Please learn more about variable scope; See: http://stackoverflow.com/q/16959576/3933332 – Rizier123 Jul 01 '15 at 11:16
  • `$z` is defined only in the function's scope, aka `$z` is not a global. When you're trying to echo out `$z` php will throw an error since it has no knowledge of `$z` outside of the function's scope. This is, however, a simple fix, just `return $z` and assign the function to a variable: `$variable = addition()` and you'll get the value of `$z` assigned to the variable `$variable`. – Andrei Jul 01 '15 at 11:16
  • @Andrew is this the only way to get the value of a variable defined inside a function ?? I can not get the value of $z outside function using $GLOBALS['z'] since it is not a global ?? – StealthTrails Jul 01 '15 at 11:24
  • Check out @Robert 's answer, but skip the Dependency Injection part, get the basics down first. – Andrei Jul 01 '15 at 11:25
  • There are many ways to pass value from function reference, globals, singleton, values but IMO globals and singleton are the ugliest solutions – Robert Jul 01 '15 at 11:32

2 Answers2

0

If you start with programming I can highly recommend you not to use GLOBALS. There are better ways to get object/variable than globals. For example dependency injection

Function is something what takes arguments does something with them and return value. So you can create addition function

function addition ($x, $y) {
  return $x + $y;
}

when invoked this will return sum of $x and $y. When you have this value you can for example output it to user with echo

echo addition(5, 4);

or

 $z = addition(5, 4);
 echo $z;

In your case you get notice because you want to get variables that are not set. You can check if variable is set with function isset($variable)

If you still want to use $_GLOBALS then you need to know what $_GLOBALS is. It's reference for all variables in global scope so to make it work you should write something like

$x = 5;
$y = 7;

function addition() { 
    return  $GLOBALS['x'] + $GLOBALS['y']; 
}

echo addition();

But this is bad approach and I better don't do it.

Edit:

Check also this link

Robert
  • 19,800
  • 5
  • 55
  • 85
  • why does this works ? `` – StealthTrails Jul 01 '15 at 11:53
  • This is pretty hard to explain but it looks like if you assign key to $_GLOBALS it creates variable and saves reference to this variable. So $_GLOBALS['z'] returns referenced variable where you change value of this variable and you have access to this variable outside function scope – Robert Jul 01 '15 at 14:22
0

$GLOBALS is a superglobal variable which is used to access all defined variables according to their scope. In above scenario $z is variable defined within function hence its scope is limited to the function . To make it globally accessible it should be declared outside function body and should be declared as global variable within function body.

Example

$z=0;$x=20;$y=30;
function addition() { 
    global $z;
    $z = $GLOBALS['x'] + $GLOBALS['y']; 
}

addition(); 
echo $z; 
Rubin Porwal
  • 3,736
  • 1
  • 23
  • 26