Variables & Data-types in PHP

Syntax in PHP

What is a variable?

In simple words, a variable is an entity that changes its value. In programming, a variable is an entity that stores some value in its variable name in the computer memory location and you can change its value anytime. For example, a=2 (here a is variable name and 2 is its value). so variable is a name that stores values for its users in computer memory.

What is a PHP variable?

PHP variables are also like containers that store character values, number values, string values, and memory addresses.

PHP variables always change their previous value when some new value is defined to their variable. For example, if a=2, then value 2 will be printed on the screen. Now in the next line, if we will write a=3, without erasing the previous one, the program will print value 3 as an output. So, this way variables change their old values with the new ones if we give them the same variable name like a. if you want to print both 2 and 3 then you have to create two variables with different names. Suppose a and b.

But there are a few rules, that needs to be followed while dealing with variables in PHP:

  • A variable name must start with a dollar sign ($), followed by the variable name. For example, $a=” hello”. Variable name should be on left to the ‘=’ sign and its value on the right.
  • All the variables have a specific data type. Example, string, float, integer, etc.(has been discussed below)
  • A variable name can only contain alphanumeric characters and underscores (i.e., ‘a-z’, ‘A-Z’, ‘0-9, and ‘_’) in their name. Even it cannot start with a number.
  • Variable names in PHP names must start with a letter or underscore and no numbers.
  • PHP variables are case-sensitive, i.e., $sum and $SUM are treated differently.

Some correct variable names: – $Firstname, $_first_name, $first-name, $firstname12

Some incorrect variable names:- $First name, $12firstNAme

Example to write your PHP code with a variable

The output of the above code

Explanation of code: – 1. The name of our variable is ‘a’ and the value of it is ‘book’. Remember, the word book is written as “book”. You can also write it as ‘book’ i.e. in quotes. Quotes mean the type of data is a string. We will discuss it later

  • The variable name should start with the ‘$’ sign followed by a variable name.
  • Semicolon (;) should always be inserted at the end of the line to separate it from the current value.
  • Now, in the echo statement you can write your code with or without parenthesis. But it should always be in double quotes (“ ”). And a semicolon (;) at the end.
  • If you have to write any HTML code in your PHP code, then it must also be in double quotes (“ ”).

Another example to write code is (echo “my “.””.”$a”;)

Variables as numbers

Variables can also be defined in numbers. They are called integers. Example, $a=45; $b=12;

We do not write numbers in quotes.

Arithmetic operations can also be done with integer variables. For example, if you want to add two numbers, you can do it like example given below:-

echo $a+$b; 

PHP Data-Types

What is a data type?

In the above explanation, we have seen that the value of variable ‘a’ i.e. ‘book’ is written in double quotes. So, double or single quotes will be defined as string data-type.

We cannot write everything in double or single quotes as well. If we have to write numbers, they must be written without any quote i.e. neither single(‘ ’) nor double quote (“ ”)

So, we concluded that the type of data that we enter as a variable value is known as data type.

So, data-types are of different types: –

  • String
  • Integer
  • Float
  • Boolean
  • Array
  • Object
  • Null


A string is a sequence of characters, always written in quotes. like “Book”. Example, $a= “book”


An integer is a non-decimal number. Always written without quotes. Example, $a=25;


A float is a decimal point number or a number in exponential form. Always written without quotes. Example, 25.05;


A Boolean represents two conditions. When you want to check whether one condition is true or false. For example, TRUE or FALSE.


An array stores multiple values in one single variable.
When you have to show multiple values in a single variable, then arrays are used. Example, $a=array (“jump”, “ball”, “axe”);


A NULL is a variable that has no value assigned to it. It will print nothing on screen. Example, $a=null;


When we create some classes in a variable name, this is known as an object. It will be covered in the next chapters.

write your code in this way:-

Just write your code as in the example. You will find this error Parse error: syntax error, unexpected ‘;’, expecting ‘)’ in C:\xampp\htdocs\variables.PHP on line 6

Now, comment your line 6 and run your program. Now, the output will be of the variable which you have typed last which is at line 5. So, all the variables which are defined at lines 2, 3, 4 are replaced by a variable at line 5. That means every variable changes its previous value when a new value is defined to the same variable name.

So, try commenting on every variable one by one and try all data types individually. So, that you can see the difference of each data type.

(Var_dump) is used to see the data type of variable. Try commenting on it as well and then see the difference.

Variables & Data-types in PHP
Show Buttons
Hide Buttons