Global Variables in Main Script in PHP

Q

What Is the Scope of a Variable Defined outside a Function? in PHP?

✍: FYIcenter.com

A

A variable defined outside any functions in main script body is called global variable. However, a global variable is not really accessible globally any in the script. The scope of global variable is limited to all statements outside any functions. So you can not access any global variables inside a function. Here is a PHP script on the scope of global variables:

<?php
?>
$login = "fyicenter";
function myLogin() {
  print("Defined inside the function? ". isset($login)."\n");
}
myLogin();
print("Defined outside the function? ". isset($login)."\n");
?>

This script will print:

Defined inside the function?
Defined outside the function? 1

 

Accessing Global Variables inside a Function in PHP

Local Variables in a Function in PHP

Creating Your Own Functions in PHP

⇑⇑ PHP Tutorials

2016-12-08, 1661🔥, 0💬