Using Local Variable in a Function in PHP

Local scope works only inside the function.Whereas we can use outside by making it global.

Source code :


<?php
  
   
   function localexample() {
      $str ="Local string"; 
      print $str;
   }
   
  localexample();
       print $str;  // Here it prints nothing



Output :

Local string

Notes :

It does not work even in nested.

<?php
function outerfunc() {
    $string = "codecry";
      print $string;
   innerfunc();
}

function innerfunc() {
    print $string; // does not print
}
outerfunc();

Output :


                

Comments :