Austin's GLOBAL Examples

This is inside myFunctionOne
Printing a GLOBAL ARRAY variable, namely: Austin

This is inside myFunctionTwo
Printing another GLOBAL ARRAY variable, namely: Austin

This is inside myFunctionThree
Printing a global declared variable, namely: Austin

This is inside myFunctionFour

This function is trying to print a 'local to the main script' variable, >>><<<
The above functionFour() will generate an ERROR, like so (and above):

Notice: Undefined variable: name in /home/clelana5/public_html/citw185/examples/week7/global.php on line 76

Code follows

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Global Arrays</title>
    <link rel="stylesheet" href="../css/styles.css">
</head>
<body>

<div class="page">

    <p class="nav"><a href="../../index.php">Home</a></p>

    <?php

    
// code
    
error_reporting(E_ALL);    // set error reporting to all

    
echo("<h1>Austin's GLOBAL Examples</h1>");

    
// ==== main script =====

    
$name = 'Austin';                            // $name is local
    
$GLOBALS['var'] = $name;             // var is Global

    
myFunctionOne();        // call to function one
    
myFunctionTwo();        // call to function two
    
myFunctionThree();        // call to function three
    
myFunctionFour();        // call to function four

    // ==== functions =====

    // ---- myFunctionOne -----
    // This function pulls a varable from the GLOBALS array
    // and prints it

    
function myFunctionOne()
    {
        
$a = $GLOBALS['var'] ;
        echo(
'<p>This is inside myFunctionOne<br>');
        echo(
"Printing a GLOBAL ARRAY variable, namely: $a</p>");
    }

    
// ---- myFunctionTwo ----
    // This function pulls a varable from the main script via $GLOBALS['name']
    // and prints it

    
function myFunctionTwo()
    {
        
$a = $GLOBALS['name'];        // this demonstrates that all local variables are Globals IF they are pulled from the GLOBAL array
        
echo('<p>This is inside myFunctionTwo<br>');
        echo(
"Printing another GLOBAL ARRAY variable, namely: $a</p>");
    }

    
// ---- myFunctionThree ----
    // This function identifies/links the variable $name as being global
    // and prints it

    
function myFunctionThree()
    {
        global 
$name;        // You can define the local variable to be GLOBAL and use it
        
echo('<p>This is inside myFunctionThree<br>');
        echo(
"Printing a global declared variable, namely: $name</p>");
    }

    
// ---- myFunctionFour ----
    // This function attempts to pulls a varable from the main script
    // but fails to print it
    // NOTE: This will produce a:
    // Notice: Undefined variable: name in /home/sperlt/public_html/citw185/examples/global.php on line whatever

    
function myFunctionFour()
    {
        echo(
'<p>This is inside myFunctionFour</p>');
        echo(
"<p>This function is trying to print a 'local to the main script' variable,  >>>$name<<< <br>");
        
$a = __LINE__;
        
$a--;
        echo(
"The above functionFour() will generate an ERROR, like so (and above):</p>");
        echo(
"Notice: Undefined variable: name in " . __FILE__ . " on line $a");
    }

    
?>

    <hr>

    <h2>Code follows</h2>

    <div class="code"><?php highlight_file(__FILE__); ?></div>

    <p class="nav nav-foot"><a href="../../index.php">Home</a></p>

</div>

</body>
</html>