Variables in any programming language is a way to temporary store data. They can store numbers, letters and any characters. Once the variable is declared it can be used over & over again in your program/script.

Variables are reset each time the script/program is loaded hence why they are only temporary data storage.

Declaring & setting a variable
All variables in PHP must begin with a $.

Text (string)
The below example is how you would declare and set a text variable (in technique term, this is know as a string). String variables must be wrapped in quote marks. Double & single quote will work, but try to be constant and stick to either one.

$myVariable = "Hi there";

$myVariable = ‘Hi there’;

Using one double and one single quote is invalid and will cause your script to fail. You must start and end with the same thing. And remember that semi-colon at the end.

$myVariable = "Hi there’; // invalid syntax

Number (integer)
Declaring and setting a number variable it is very simple (in technique term this is know as integer). There is no need for any quote marks like for a string. The below example will show you how to declare an integer variable.

$myVariable = 10;

You can declare a variable without setting any value to it. Both the methods shown below is valid ways of declaring a blank variable.


$myVariable = " ";

If you don’t set your variable to anything it will default it to 0.

Rules for naming a variable
PHP doesn’t allow you to name the variables anything you want. They have a few rules on what is and is not allowed.

  • A variable name must start with a letter or an underscore “_”
  • A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ )
  • A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_variable), or with capitalization ($myVariable)