0

Its very simple code but i don't understand whats the point :(

function aaa($b){
$a = 30;
global $a,$c;
return $c=($b+$a);}
echo aaa(40); //Output is 40

Why the output is 40 ? When i call the $a outside of function it gives me the desired answer so what the point ?

$a = 30;
function aaa($b){
global $a,$c;
return $c=($b+$a);
}
echo aaa(40); //Output is 70


function aaa($b){
global $a,$c;
$a = 30;
return $c=($b+$a);
}
echo aaa(40); //Output is 70
IqbalBary
  • 1,086
  • 1
  • 11
  • 17
  • 1
    That is because [Global variables are never the correct answer](http://stackoverflow.com/questions/5166087/php-global-in-functions/5166527#5166527) – Gordon Apr 20 '13 at 07:42
  • $global will not work any more in PHP 5.4+ - thank's god, the sh.. is gone. :-) – herrjeh42 Apr 20 '13 at 08:39

4 Answers4

1

See what the global keyword stands for here:

function aaa($b) {
    $a = 30;               # local variable, will be overwritten just in the next line
    global $a, $c;         # importing $a from the global scope - NULL
    return $c = ($b + $a); # $c = (40 + NULL)
}

The manual at http://php.net/global reminds that a global variable influences the function if it is used inside there. That is not only for the first call but also for all subsequent calls.

This makes the function non-deterministic, so less useful code and you might - as you just did - get irritated by it.

An easy way out is: Instead of putting parametric values of that function into global variables, turn them into parameters:

function aaa($a, $b, &$c) {
    return $c = ($b + $a);
}

echo aaa(30, 40, $c); //Output is 70

The code is not only much easier to read but also deterministic. It will always behave the same when called. That means less guessing.

This example still has the problem of returning a value by a parameter (by reference), however you might see as well that this is not really necessary any longer:

function aaa($a, $b) {
    return ($b + $a);
}

echo $c = aaa(30, 40); //Output is 70

Lessons to take away:

  • Do not use global variables inside functions - they only stand in your way.
  • Reduce the number of parameters a function has (that are about all incomming values, so global counts as well).
hakre
  • 193,403
  • 52
  • 435
  • 836
  • So if i define a variable before global(within func), it will be NULL ? – IqbalBary Apr 20 '13 at 07:49
  • 1
    No, `$a` is NULL because in the global scope it is NULL (by default in PHP all unset variables are NULL). `global $a` in the function tells PHP to use `$a` from the global scope inside the function. Therefore the once local variable now has the value from the global variable (NULL). – hakre Apr 20 '13 at 07:50
  • Also you should not wonder only why this does happen here, but also follow a much more important question: Why do you *need* a global variable here? – hakre Apr 20 '13 at 07:51
  • Yeah i know that, when to use `global` variable but i got that question within a mocktest and after that i posted that question. – IqbalBary Apr 20 '13 at 08:29
1

When using for example global $a;, PHP overwrites the variable $a and assigns to it a reference, as if this statement is replaced by:

$a = &$GLOBALS["a"]; // and the same with every other variable which is used in global

And as $a isn't defined in your first example, $GLOBALS["a"] evaluates to null and so $a is becoming a reference to a variable holding a null value.

Try to var_dump($GLOBALS); after and before the function call. It will show you a new index named a after the function call (with the value null or the value you've assigned it to).

(P.s.: In reality it's doing a direct reference to the variable in the main scope.)

bwoebi
  • 23,637
  • 5
  • 58
  • 79
0
in php a variable Scope priority is 
 1- local variable
 2- global variable

even you determine global $a in function body but local variable $a used when it apear. refer to Php variable reference if you want use global variable with same name of your local variable you must user $GLOBALS['c']=$GLOBALS['a']+$b; if $c and $a is global definition hope this help

mojtaba
  • 339
  • 1
  • 4
  • 17
0

I think, in first case $a is undefined. First write value of $a then call function. Use error_reporting(E_ALL) at top of page to know errors in script.

web2students.com
  • 307
  • 2
  • 16
  • Even `$a` is undefined in the global scope, the global keyword does define it in the local scope so you won't see that warning. – hakre Apr 20 '13 at 07:58