Understanding variable scopes in php

后端 未结 4 486
太阳男子
太阳男子 2020-12-18 14:17
echo \"Point1, a=\".$a.\"\\n\";
echo \"Point1, b=\".$b.\"\\n\";
if(1<2)
    {
        $a = 6; 
        $b[\'link\'] = \"here\";
        echo \"Point2, a=\".$a.\"\         


        
4条回答
  •  -上瘾入骨i
    2020-12-18 14:47

    In PHP only functions introduce a new scope, unlike Java and other languages :((( So If you need a new scope you need to create a new function and call it.

    $a = 1;
    
    
     call_user_func(function(){
         //new local scope
         $a = 99999;
    });
    
    
    echo $a;   //will print 1
    

提交回复
热议问题