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
2
  • 1
    That is because Global variables are never the correct answer Commented Apr 20, 2013 at 7:42
  • $global will not work any more in PHP 5.4+ - thank's god, the sh.. is gone. :-) Commented Apr 20, 2013 at 8:39

4 Answers 4

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).
Sign up to request clarification or add additional context in comments.

4 Comments

So if i define a variable before global(within func), it will be NULL ?
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).
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?
Yeah i know that, when to use global variable but i got that question within a mocktest and after that i posted that question.
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.)

Comments

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

Comments

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.