In JavaScript a global variable is declared outside of any functions and then can be "seen" by any code inside or outside of any function. In PHP global variables are available to all functions but the functions will not "see" them unless you tell them to.

Create a new PHP page called phpscope.php from your PHP template.  Place this at the top just after the include_once line which you recently added:

function thisfunction() {
    echo $message;

That creates a global variable and you might expect it to work.  Add this line inside the PHP just under your h1 element lower in the code:


Save, upload and try and you will see one of two errors:

  1. an error that the variable does not exist (which it does but not within the scope of thisFunction)
  2. an error (or two) which mentions include_once

Getting the second error means you have forgotten how PHP works.  For the external files to be included they need to be on the server (did you upload them?).

Getting the first error is expected.  This is a safety precaution to stop people writing functions which overwrite the existing date held in global variables.  In some languages if you did this:

var message="hi";
function thisfunction() {

    echo message;

$message would exist as a global.  The line inside the function would change it's value.  If any other function uses that global variable it will get a number instead of the text it expected.  This is one of the reasons you should not use global variables!

If you really have to use globals in PHP you can but you have to specifically tell the server you want to.  Amend your code by adding this new line above the echo line:

global $message;

Try it again and it should now echo the number.  As mentioned it is normally better to pass the data to the function as an argument and then return any changed value rather than to use global variables.