-2
<?php
   $a ="helllo welcome to php! ....:)"; 
   function msg(){ 
       /* global $a; */
       echo $a;
   } 
   msg();
?>

Global variable $a is not accessible inside the function msg() but when i use global $a then it is accessible, i want to know that it is accessible or not without using global keyword inside or outside the function for the particular variable

Output

Code

✔ In python, it's accessible even without using the global keyword ✔
But it is supported in python even without using global keyword ==> see the output at the right

Shubham-Misal
  • 33
  • 1
  • 7

2 Answers2

-1

Why did you commented global $a ?

<?php
   $a ="helllo welcome to php! ....:)"; 
   function msg(){ 
       global $a;
       echo $a;
   } 
   msg();
?>
ibra
  • 390
  • 3
  • 13
-1

If you want to check for the variable's availability then your can use isset($a).

I'm going to be honest and say that you shouldn't do that though. It doesn't make sense in any way.

The functions scope doesn't change and suddenly allow for the $a to be available. You should either use the global keyword, or pass the variable as an argument to the function.

Morten H
  • 150
  • 5