• Global variables
  • Static variables
  • Passing variable to function by reference

Variable scope

The scope of a variable is the context within which it is defined and can be used.
The variables used in PHP functions can be of three types: locals, globals and statics
Any variable defined inside a function is by default limited to the local function scope, is available only in the code within that function. For example:

<?php
function test() {
  $x = 8;
  echo $x;
}

echo $x;             // Notice: Undefined variable: x in C:\server\www\file.php on line 7
test();              // 8
?>
The "echo $x" expresion outside the function returns an error becouse the $x variable isn't defined or recognized outside the test() function, but the "echo" statement inside function refers to a local $x variable, wich is defined within test() function.
Another example:
<?php
$x = 8;

function test() {
  echo $x;
}

echo $x;             // 8
test();              // Notice: Undefined variable: x in C:\server\www\file.php on line 5
?>
This time, the $x variable is defined outside the function, but not inside it.
As you can see, the calling function returns an error notice, because the "echo" statement refers to a local version of the $x variable, and it has not been assigned a value within this scope.

Global variables

A global variable can be accessed in any part of the program.
If you want to use inside a function a variable defined outside it, that variable must be explicitly declared to be global in the function. This is accomplished by placing the keyword GLOBAL (or global) in front of the variable that should be recognized as global (or more variables separated by comma). This will extends the scope of that variable inside the function.
  - Example:
<?php
$x = 8;
$y = 9;
$total = 0;

function getSum() {
  GLOBAL $x, $y, $total;

  $total = $x + $y;
  echo $x. ' + '. $y. ' = '. $total;
}

getSum();                         // 7 + 8 = 17
echo '<br />'. $total;            // 17
?>
By decaring $x and $y variables GLOBAL within the function, the values of these variables can be used inside the function.
The $total variable being set as GLOBAL, all references to it, inside or outside function, refer to the same global version. Changing its value within the function will carry forward.
The code above will output:
7 + 8 = 17
17

Static variables

A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.
Declaring a variable static makes its value to be remembered by a function for the next time it is called.
To declare a variable static, place the keyword STATIC (or static) in front of the variable (or more variables separated by comma).
  - Example:
<?php
function f_local() {
  $x = 0;
  echo '<br /> x = '. $x;
  $x++;
}

// Function with STATIC variable
function f_static() {
  STATIC $x = 0;
  echo '<br /> x = '. $x;
  $x++;
}

// Calling the f_local() function
f_local();                           // 0
f_local();                           // 0
f_local();                           // 0

echo '<hr />';

// Caling the f_static() function
f_static();                          // 0
f_static();                          // 1
f_static();                          // 2
?>
Every time the f_local() function is called it sets $x to 0 and prints 0. The $x++ which increments the variable serves no purpose since as soon as the function exits the value of $x variable disappears.
In the f_static() function, $x is initialized only in first call of function and every time the f_static() is called it will remember the value of $x, and will print and increment it. This code will output:
x = 0
x = 0
x = 0
x = 0
x = 1
x = 2

Passing variable to function by reference

The arguments transferred to a PHP function are passed by value, which means that the values of the arguments are copied ​​and the function uses copies of their values, not the variables themselves, so, the function can't change their values.
However, you can pass a variable by reference to a function so the function can modify the variable.
For this to work, you have to add an ampersand (&) character in the function's definition code to the variable being passed by reference.
  - Example:
<?php
function test(&$var) {
  $var += 5;          // adds 5 to $var
}

$x = 3;

test($x);
echo $x;                 // 8
?>
The test() function changes the value of the variable passed as a argument to it.
The construction &$var tells PHP to pass the value of the argument to the function and at the same time extend the scope of that argument variable into the function so that when the function ends its work, any changes that were made to that variable will carry forward.
After the test() function is called, the value of $x variable (used as argument) is changed, and the expresion "echo $x;" prints "8".

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag adds an image in web page?
<div> <img> <span>
<img src="http://coursesweb.net/imgs/webcourses.gif" width="191" height="63" alt="Courses-Web" />
Which of these CSS codes displays the text oblique?
font-style: italic; text-decoration: underline; font-weight: 500;
#id {
  font-style: italic;
}
Click on the jQuery function used to hide with animation a HTML element.
click() hide() show()
$(document).ready(function() {
  $(".a_class").click(function(){ $(this).hide("slow"); });
});
Click on the correctly defined function in PHP.
fname function() {} function fname() {} function $fname() {};
function fname($a, $b) {
  echo $a * $b;
}
Functions, Variable scope and Passing by Reference

Last accessed pages

  1. Plant House (99)
  2. Zuma Games (8842)
  3. querySelector and querySelectorAll (3613)
  4. Escape Games (3376)
  5. Dog Mahjong (1229)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (47860)
  2. Read Excel file data in PHP - PhpExcelReader (35978)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (35586)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (32087)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (29359)