Using global vars within a function in PHP the way you do it in Javascript

后端 未结 5 1854
渐次进展
渐次进展 2021-01-27 15:22

I have a function that uses lots of global vars and arrays - e.g.

$a=1;
$b[0]=\'a\';
$b[1]=\'b\';
$c=\'Hello\';

function foo() {
 echo \"$a 
       $b[0] 
              


        
5条回答
  •  再見小時候
    2021-01-27 15:26

    You should use the "global" keyword, which tells the compiler to look for global variables.

    function foo() {
      **global $a, $b, $c;**
     echo "$a 
           $b[0] 
           $b[1] 
           $c";
    }
    

提交回复
热议问题