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 in <table> to create table header cell?
<thead> <th> <td>
<table><tr>
  <th>Title 1</th>
  <th>Title 2</th>
</tr></table>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
}
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
  window.open("http://coursesweb.net/");
}
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
var_export($ar_dir);
Functions, Variable scope and Passing by Reference

Last accessed pages

  1. Animated Tooltip with HTML and CSS (284)
  2. MySQLDumper - Backup MySQL Database (1782)
  3. querySelector and querySelectorAll (30136)
  4. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142540)
  5. JavaScript strip_tags and stripslashes (8825)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (543)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (63)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)