Programs need to store data.  In PHP you did this:

$name="Fred";
echo "<p>Hello ";
echo $name;

This put the text into a variable to use it as a container for data.  That text could then be used by referring to the variable.  This allows the same data to be used more than once.  Remember to use sensible names for variables.  Names which explain what is held in the variable and which do not use strange symbols.  If you use long names you are less likely to accidentally use the same name twice.

Variables

The data inside variables can be changed.  So you could do this:

$name="Fred";
echo "<p>Hello ";
echo $name;
$name="Sara";
echo "<p>Hello ";
echo $name;

The second time the variable is output to the page the data is different.

Constants

Many programming languages have ways to store data but protect it from change.  These are the same as variables but you cannot change the data inside once it is there.

You have not learned how to do constants yet but as they work the same way as variables you should understand the concept.  You put data in and use it later.

Literals

In the example code above the text inside a pair of quotation marks is known as a literal.  In these examples you have string literals (e.g. Fred).  The are the actual, or literal, data.  You can also have number literals and other types.

Declaring and assigning

Before using a variable or a constant you have to create it.  This is called declaring it.  In PHP you don't really declare variables as the interpreter will do that for you any time it sees a new variable.  In JavaScript you do declare like this:

var name;

Once the variable exists you can put data into it which is known as assigning data to the variable:

name='Sara';

Normally you will do the two in one line to declare and assign:

var name='Sara';

Then later in your code you might use the same variable again to change the value (to assign new data):

name='Sara Jones';

You do not need to declare it again because it already exists.

Variable scope

If you declare a variable inside a function it is only available inside that function.  This is known as a local variable.  If you declare a variable outside of a function it depends on the programming language.  In some you can then use that same variable anywhere in the code and it is known as a global variable.  In others you have to actually label it as a global variable when declaring it.

This is important because you might use a variable inside a function and then try to use it in another one.  There are two problems which are the opposite of each other:

Think about built in functions and library functions and you should realise that they will be using variable names as well so how will you know which ones to avoid?

Why scope doesn't matter

The general advice is to only use local variables.  If you want data to be available in other functions or more generally you can pass the data around using parameters as you have done.

Functions can also return data once they have run.  This means you can send out data from the function back to the place where you called the function.  You have not done this yet but you will.

Globals are dangerous because you might use a global variable name which someone has already used inside a library function.  Then you have two variables with the same name and data can be accidentally changed.  Local variables are safe because they only exists inside their particular function.  You can have three functions using the variable name and they will not cause problems.