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:

<?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 is used to add definition lists into a <dl> element?
<dt> <dd> <li>
<dl>
 <dt>HTML</dt>
  <dd> - Hyper Text Markup Language</dd>
  <dd> - Language for web pages</dd>
</dl>
Which CSS property can hide an element on page, letting an empty space in its place?
display position visibility
#id {
  visibility: hidden;
}
Click on the event which is triggered when the mouse clicks on an object.
onclick onmouseover onfocus
document.getElementById("id").onclick = function(){
  alert("http://CoursesWeb.net/");
}
Indicate the PHP variable that contains the contents of both $_GET, $_POST, and $_COOKIE arrays.
$_SESSION $_GET $_REQUEST
if(isset($_REQUEST["id"])) {
  echo $_REQUEST["id"];
}
Functions, Variable scope and Passing by Reference

Last accessed pages

  1. Get CSS property value with getComputedStyle ot jQuery (5533)
  2. The Four Agreements (1619)
  3. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (55417)
  4. ActionScript 3 Lessons (7252)
  5. Get Lower, Higher, and Closest Number (5331)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (252)
  2. Read Excel file data in PHP - PhpExcelReader (91)
  3. The Four Agreements (76)
  4. PHP Unzipper - Extract Zip, Rar Archives (76)
  5. The Mastery of Love (66)
Chat
Chat or leave a message for the other users
Full screenInchide