VARIABLE SCOPE - a Local Variable Declared Outside of Function

Chapter 7 #137 - Martha's CIS 52

ASSIGNMENT/EXPLANATION

"A variable declared within a function remains local to that function. In other words, it will not be available outside the function or within other functions." (137) This example illustrates what happens when a programmer tries to access a local variable that "does not exist". "The value of the variable $testvariable is not printed when it is called, because no such variable exists outside the test() function. A valuable lesson...

Exercise

The code printed below has been removed from this page because it causes errors! See what happens when it IS included on a webpage: scopetest.php. The code, printed below, is definitely worth studying...

CODE

<?php 
function test() {
$testvariable = "This is a test variable.";
}
echo $test variable: ".$testvariable."<br />";
?>