Variables in PHP

This tutorial explains what variables are, how to declare variables, and how to assign values to variables in PHP. It also explains how to include variables in an echo statement.

A variable is used to store data in a program. Variables are given a unique name when they are created and can be assigned values of different types. A variable name should not be the same as an existing function that already exists in PHP or the same as another variable that already exists in the program.

Variable names can contain uppercase and lowercase letters and numbers as well as underscores but should start with a letter. In PHP, variable names always start with a $ character eg. $myVariable. Variable names cannot contain spaces.

The main types of data variables can store in PHP are:

  • integer – whole numbers
  • float – numbers with a decimal point
  • string – text which can include letters, numbers, and other characters
  • boolean – true or false values

You can create a variable without immediately giving it a variable, for example: $myVariable; However, variables can be created and assigned a value in one statement eg: $number = 5; or $sentence = “Hello world”;

If you want to give a variable a name which contains more than one word, you can join the words together and use the camel case format where each new word starts with an uppercase letter eg: $myStringVariable;

If you wish to display the value stored in a variable you can use an echo statement eg: echo $myVariable;

Watch the video below to see how to create and use variables, and then scroll down to see the sample code.

There are a few rules to naming variables:

  • Names of variables must begin with a $ dollar sign
  • Letters, numbers and underscores can be used in variable names but no spaces
  • The first character of a variable name cannot be a number
  • Variable names are case-sensitive (that means, $myvariable, $myVariable, and $MYVARIABLE are three different variables).

Sample PHP code:

 // Variables are given a name after a dollar sign eg. $myvariable
 // Variables are assigned a value after the equals sign (assignment operator)
 // Variable declarations end with a semicolon
 $name = "Batman"; // this variable holds a string value
 $age = 25; // this variable holds an integer value
 echo "My name is " , $name , " and I am " , $age , " years old."; // concatenated string

The above code is an example of concatenating (joining) a variable and string together. With concatenation, you can join variables and strings in a few different ways. The following different combinations of commas, periods and brackets will work:

 echo "My name is " , $name , " and I am " , $age , " years old.";
 echo ("My name is " . $name . " and I am " . $age . " years old.");
 echo "My name is " . $name . " and I am " . $age . " years old.";

However, you cannot use the comma method of concatenation if the echo statement is placed inside brackets. Eg. the following will NOT work:

 echo ("My name is " , $name , " and I am " , $age , " years old.");