PHP Variable

A variable is a container that stores values, values can be of any PHP supported data types.

$movie_name = 'Gladiator'; //string value data type $release_year = 2000; //integer data type (whole number) $cost_now = 7.96; //float / double data type (fractional number) $awesome = true; //boolean data type (true or false only)

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.

echo "The movie $movie_name was released in $release_year and it now costs $$cost_now at amazon.com on blue ray. "; echo $awesome ? "The movie is awesome!" : "The movie is okay...."; //output: The movie Gladiator was released in 2000 and it now costs $7.96 at amazon.com on blue ray. The movie is awesome!

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:

//The following are 3 different variables: $name = 'Atari'; $Name = 'Sega'; $NAME = 'Nintendo'; echo $Name; //output: Atari echo $name; //output: Sega echo $NAME; //output: Nintendo

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:

$_valid = "This is okay to do";

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.

$year_now = date( "Y" ); //gets current year $years_ago = $year_now - $release_year; echo "The $movie_name was released $years_ago years ago!"; //output: The Gladiator was released 17 years ago!"

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:

$num1 = 10; $num2 = 30; $average = ( $num1 + $num2 ) / 2; echo $average; //output: 20

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.

$my_age = 33; echo "Later this year I will be ".++$my_age; //output: Later this year I will be 34

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.

echo "Last year I was ".--$my_age; //output: Last year I was 32

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.

$my_age = 33; echo "I am ".$my_age++." years old now"; //output: I am 33 years old now echo "On July I'm turning $my_age"; //output: On July I'm turning 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.

The most common built-in PHP variable handling functions designed to work with variables.

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:

$movie_name = 'Gladiator'; $release_year = 2000; $cost_now = 7.96; $awesome = true; echo gettype( $movie_name ); //output: string echo gettype( $awesome ); //output: bool echo is_int( $release_year ); //output: 1 echo is_float( $cost_now ); //output: 1 echo isset( $awesome ); //output: 1 echo intval( $cost_now); //output: 7 var_dump( $movie_name ); //output: string(9) "Gladiator" unset( $movie_name ); echo $movie_name; //Notice: Undefined variable: movie_name echo empty( $movie_name ); //output: 1

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:

function microscope() { //local scope $local = 'Hi I\'m $local: I live only inside the function!'; echo $local; } //trying to access local variable from a global scope echo $local; //Notice: Undefined variable: local microscope(); //output: Hi I'm $local I live only inside the function!

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:

$global = "hola"; function microscope() { echo $global; } microscope(); //output: Notice: undefined variable: global

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":

function microscope() { global $real_global; $real_global = "Hi I\'m all over the place!"; echo $real_global; } echo $real_global; //output: Hi I'm all over the place!

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:

function solar_system() { static $planets = "Earth, Mercury"; echo $planets; $planet_one = "Mars"; $planet_two = "Venus"; $planets = $planets.', '.$planet_one.', '.$planet_two; } solar_system(); //output: Earth, Mercury solar_system(); //output: Earth, Mercury, Mars, Venus

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:

function solar_system() { $planets = "Earth, Mercury"; echo $planets; $planet_one = "Mars"; $planet_two = "Venus"; $planets = $planets.', '.$planet_one.', '.$planet_two; } solar_system(); //output: Earth, Mercury solar_system(); //output: Earth, Mercury

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.