What Is the Scope of a Variable Defined in a Function

Q

What Is the Scope of a Variable Defined in a Function? - PHP Script Tips - Creating Your Own Functions

✍: FYIcenter.com

A

The scope of a local variable defined in a function is limited with that function. Once the function is ended, its local variables are also removed. So you can not access any local variable outside its defining function. Here is a PHP script on the scope of local variables in a function:

<?php
?>
function myPassword() {
  $password = "U8FIE8W0";
  print("Defined inside the function? ". isset($password)."\n");
}
myPassword();
print("Defined outside the function? ". isset($password)."\n");
?>

This script will print:

Defined inside the function? 1
Defined outside the function?

2007-04-24, 4596👍, 0💬