PHP Variable
A variable is a container that stores values, values can be of any PHP supported data types.
We can then use variable names to print out the values they hold to the screen using keyword echo (also known as an output statement). When placing variables inside a string, it's important to use double quotes, doing so will print out variable values, which is what we want. Placing variable names inside the string in PHP is called interpolation and is worth learning about from the beginning.
I also used ternary operator ? : in the above example to check if the value of $awesome is equal to true. If it's true "The movie is awesome!" is printed, if not "The movie is okay...." would be printed. A ternary operator is the shortest version of conditional expressions in PHP.
Naming Conventions
Here is a simple example which shows what case sensitive variables mean in PHP:
Just make sure you never start a variable name with a special character or a number, you'll get an error, because PHP doesn't allow that, you can use an underscore though:
Also, not a strict rule, but rather good programming practice is to name your variables in a similar style, usually either $CamelCaseFormat, or $under_score_format. As you can see in the examples across this web site I prefer to use the underscore format and use all lower case letters in a variable name.
Modifying Variables
Variable values often change (vary). There are many different functions and operators to assign, modify, compare and check variable values. In the following example, we find out how long ago the movie Gladiator was released by using a subtraction operator.
A built-in function date() with a string parameter "Y" was called to get the current year. We then used the subtraction operator to subtract current year from the release year which gave us how many years ago the movie was released. In fact PHP supports all arithmetic operators. Let's find the average of two numbers using a couple of operators:
Pre-Increment & Post-Increment
One very common operator that you will encounter a lot is ++ (plus plus), which simply increments the variable value by 1.
Just in case you noticed that . dot in front of ++$my_age, let me explain myself :) The dot in PHP is called "concatenate" which simply means, join the two together. We have to use concatenate whenever there is anything more complex going on like calling a function, using increment/decrement or any other operation beyond simply using a variable name. If that didn't make any sense, you can read more about concatenate.
In a similar way -- (minus minus) decreases the value of variable by one.
Let's also clarify the difference between ++$my_age (pre-increment) and $my_age++ (post-increment). In the case of pre-increment which is the example above, the value of the variable is incremented by one and then returned, so we get 34. In case of $my_age++ the original value of 33 would be returned and printed to the screen, and then it would be incremented by 1. Post-increment is used all the time in PHP For Loops.
Below is an example of a post-increment, notice the difference from earlier pre-increment example, the value of $my_age returned in the first echo output statement is still 33 (the original value is returned), and is only then incremented by one in the second echo statement, giving us 34.
PHP Variable Handling Functions
There are many PHP built-in functions to work with variables, let's look at some of the most useful and common ones.

I think most of the above functions should be pretty self-explanatory. One thing to note is the keyword just before the function name, colored in purple (string, bool, void, int). This is simply the data type of the value which will be returned by the function after we call it on a variable. Let's use same variables from the first example of this lesson and run some of these functions on them:
As you can see all the value data types in the output are exactly the same as indicated by the return type (keyword in purple before the function name) mentioned earlier.
The function intval(); above converts a fractional (float) number into an integer. Function var_dump(); outputs: string(9) "Gladiator", which is the data type of the variable - string, 9 is the number of characters that string has and finally the content of the string itself - "Gladiator". After using function unset(); the variable $movie_name is destroyed, we try to echo it out and get "undefined variable" notice as expected. We then use empty(); function to check if the variable $movie_name does not exist, in fact it doesn't exist so the output is 1 (true).
Feel free to play around with the rest of the functions!
Variable Scope
So far, all the variables we have defined in this tutorial have global scope, meaning they are accessible anywhere in the program. However, in PHP, variables which are declared inside a function have local scope, meaning they are only accessible within the function where they have been created. Let's take variable scope under the microscope:
Please keep in mind that "global" scope only refers to any area in the program outside of a function. Any variable defined outside of a function which is said to have global scope will still be invisible inside a function:
But! There is, of course, a way to create a variable which has access within the function it has been created, as well as outside the scope of the function, we can use keyword "global":
Note how I used the keyword global following the variable name $real_global but didn't assign any value to it on the first line, this is for a reason. It's because we first have to use keyword global and only then initialize (assign a value) to the variable.
Static Keyword
Lastly, let's go over static keyword. Whenever you call a function in PHP, any variable that was created when the function was called is destroyed, that is unless we declare that variable using static keyword:
By using static, the value of the variable is retained so when we call the function a second time, we see the added values of the other two planets. This concept may be a bit confusing, so let me clarify with a non static example:
Any modifications done to the variable after it has been printed with echo have been lost, so the other two planets aren't printed even when calling a function second time, this is how all variables behave within functions without static keyword.
PHP Variable Vocabulary
Here is a list of new vocabulary words that were used in this lesson, I think it's important to learn programming lingo from the beginning because it will help you learn new programming languages and concepts faster and you'll avoid a lot of confusion.
