The local variable works opposite to the Global variable. The local variable allows you to store information locally. The function tag restricts the access of the variable value from the local variable.
Example:
[php]<?php
function mddir() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
mddir();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>[/php]
The above code has two echo tags. One is in between the functions curly bracket which is in the local state. You can get the variable information from the function mddir() from the function. The php will not restrict you to call this information.
Another echo tag is available after the function mddir()
is called. The second echo tag is placed outside of the function area. It will not allow getting the “$x
” value from the variable. You can only get the $x value for the second echo tag by declaring the global value.