2
<?php

   function a(){

     $a = "hello";
      function b(){
          global $a;
          echo $a . " World";
      }
      b();
   }

   a();

?>

This is my code, it only echo " World" even I have use the global keyword to include the $a. Why?

MrTux
  • 32,350
  • 30
  • 109
  • 146
dramasea
  • 3,370
  • 16
  • 49
  • 77
  • This is interesting question, hovewer we should evoid nesting functions as much as possible. Why You try to declare function b inside a? Of course the question must be answered, and this is interesting, but the good practice is just not doing it :). +1 .from me. – Jacek Kowalewski Nov 22 '14 at 09:57
  • I'm just playing around with php and find out this problem :) – dramasea Nov 22 '14 at 09:58
  • OK ;). Interesting question: http://stackoverflow.com/questions/415969/what-are-php-nested-functions-for. I will try to figure that out. I think that global is just for the variables OUTSIDE everything. So in that case $a is local in a() contest, and that is why it is not global in b() contest :). But It is just a guess. – Jacek Kowalewski Nov 22 '14 at 09:59
  • 1
    Does it work when you omit global $a? It looks to me like you are declaring $a within the function a(), but then in b are referencing the global $a, something different from $a within a(). I'd like to run a quick test but alas, have to reboot – Ben A. Hilleli Nov 22 '14 at 10:00
  • @JacekKowalewski that sounds logic, lol – dramasea Nov 22 '14 at 10:03
  • @BenA.Hilleli without global it won't works – dramasea Nov 22 '14 at 10:05

3 Answers3

4

You have to tell which variable from the outer scope you want to use.

<?php

   function a(){

     $a = "hello";
      function b() use ($a){
          echo $a . " World";
      }
      b();    }

   a();

?>
mathk
  • 7,973
  • 6
  • 45
  • 74
0

function a call first after call call function b. if you pass parameter in function b or set as globel $a in function a

set as globel $a in function a() because this variable use in finction b without passing parameter and set globel $a in function b because not define this variable this function

<?php

   function a(){
     global $a;
     $a = "hello";
      function b(){
         global $a; 
          echo $a . " World";
      }
      b();
   }

   a();
?>

or you can use

<?php

   function a(){

     $a = "hello";
      function b($a){

          echo $a . " World";
      }
      b($a);
   }

   a();

?>
Manish Jesani
  • 1,339
  • 5
  • 20
  • 36
  • 1
    What is the difference? Could you explain it more? – Aycan Yaşıt Nov 22 '14 at 10:00
  • 2
    Yep, This is the same as declaring $a before a(). And that's why $a is global in that case, and isn't in the example in Q. – Jacek Kowalewski Nov 22 '14 at 10:01
  • function a call first after call call function b. if you pass parameter in function b or set as globel $a in function a – Manish Jesani Nov 22 '14 at 10:02
  • 3
    Be vary careful with this answer. It radically change the semantic. Now the variable `$a` is scoped globally and accessible from outside the function `a` – mathk Nov 22 '14 at 10:08
  • And that is the answer why OP can't access `$a` in his example. His var is just not defined in the `global` namespace thus is undefined in `b` – DarkBee Nov 22 '14 at 10:11
  • Well it depend on the OP intention. If he want `$a` global. There some program that can output different result depending on whether`$a` being global or not. – mathk Nov 22 '14 at 10:19
  • Using global is not a good solution and I don't really understand why people agree with this solution by voting up this answer. – Alexandru Guzinschi Nov 22 '14 at 11:27
0

please note that variables are not passed to functions by default unless otherwise specified e.g. use

function a($b){ //code } 

instead of just

function a(){ //code } 

I think that might be the problem you are facing.

DLastCodeBender
  • 327
  • 1
  • 13