Php-mysql Course

  • 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:

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:
$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:
$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

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:
function f_local() {
  $x = 0;
  echo '<br /> x = '. $x;

// Function with STATIC variable
function f_static() {
  STATIC $x = 0;
  echo '<br /> 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:
function test(&$var) {
  $var += 5;          // adds 5 to $var

$x = 3;

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

Which tag adds a new line into a paragraph?
<b> <br> <p>
First line ...<br>
Other line...
Which CSS property can be used to add space between letters?
text-size word-spacing letter-spacing
#id {
  letter-spacing: 2px;
What JavaScript function can be used to get access to HTML element with a specified ID?
getElementById() getElementsByTagName() createElement()
var elm = document.getElementById("theID");
var content = elm.innerHTML;
Click on the "echo" correct instruction.
echo "" echo ""; echo """;
echo "Address URL:";
Functions, Variable scope and Passing by Reference

Last accessed pages

  1. Simple arithmetic calculator in PHP (2104)
  2. querySelector and querySelectorAll (30064)
  3. array_map and array_search in JavaScript (4059)
  4. ActionScript 3 - Change MovieClip Color (8965)
  5. Display data from PHP Array, or MySQL in HTML table (26805)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (326)
  2. PHP Unzipper - Extract Zip, Rar Archives (86)
  3. Read Excel file data in PHP - PhpExcelReader (82)
  4. The Mastery of Love (78)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (65)