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 lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Functions, Variable scope and Passing by Reference

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (141749)
  2. Node.js Move and Copy file (28420)
  3. MouseEvent - Events for Mouse (2909)
  4. PHPMailer (2311)
  5. Uploading images to server with Ajax (6095)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (473)
  2. CSS cursor property - Custom Cursors (79)
  3. The Mastery of Love (70)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (62)
  5. CSS3 2D transforms (46)