July 10, 2017

Core PHP: Variables Scope (2.4)

Variables Scope

PHP variables can be declared anywhere in the script.
The scope of a variable is the part of the script in which the variable can be referenced or used.

PHP's most used variable scopes are local, global.
A variable declared outside a function has a global scope, and can only be accessed outside of a function.
A variable declared within a function has a local scope, and can only be accessed within that function.

Consider the following example.
<?php
  $name = 'David';
  function getName() {
    echo $name;
  }
  getName();

  // Error: Undefined variable: name
?>

This script will produce an error, as the $name variable has a global scope, and is not accessible within the getName() function.
Functions will be discussed in the coming lessons.

No comments:

Post a Comment