Document

Tedd's GLOBAL Examples

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

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

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

This is inside myFunctionFour

This function tries to print the local variable $name: >>>Undefined<<<

The variable $name is not defined inside this function.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PHP GLOBAL Examples</title>
    <link rel="stylesheet" href="../css/assignmentHeaderStyles.css" />
</head>
<body>

<?php
include('../header/assignmentHeader.php');

error_reporting(E_ALL);

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

// Main script
$name = 'Steph';
$GLOBALS['var'] = $name;

myFunctionOne();
myFunctionTwo();
myFunctionThree();
myFunctionFour();

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

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

function 
myFunctionThree()
{
    global 
$name;
    echo(
'<p>This is inside myFunctionThree<br>');
    echo(
"Printing a global declared variable, namely: $name</p>");
}

function 
myFunctionFour()
{
    echo(
'<p>This is inside myFunctionFour</p>');
    echo(
"<p>This function tries to print the local variable \$name: ");

    
// $name is not available inside this function.
    
echo(">>>Undefined<<<</p>");

    echo(
"The variable \$name is not defined inside this function.");
}

show_source(__FILE__);

include(
'../footer/footer.php');
?>

</body>
</html>