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.
<?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.
<?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.
<?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.
<?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.
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net