Variable in PHP: Rules for naming variable in PHP

Variable in PHP
Written by Gupta

A variable is the name of memory location which is used to store a value in main memory.

Variable in PHP: Variable is the name of the memory location that is used to store temporary value in the main memory. A variable name is also used to identify the location of the main memory where a value stored.

In other words, you can simply say that the variable is used to store different types of value for temporary.

In PHP, the variable is the same as other programming languages but a little bit different. Let’s see what is variable? Rules for naming variable in PHP?

What is the variable in PHP?

A variable is a container (storage area) which is used to store value for temporary in memory. To identify storage area location, a unique name is given to each memory location which is called variable name.

In PHP, the variable is declared with a $ sign followed by the variable name. PHP is a loosely typed language that’s why it doesn’t require any data type. It converts all data into their related data type internally. e.g. $sum, $total, $text.

Variable name in PHP
Variable name in PHP

What is the real-life example of the variable? Let’s see. In a city, there are so many houses within different blocks and each house has a unique number or name to identify their exact location. In the programming languages, the unique number or name given to the memory location is called variable.

The value of the variable can be changed during the program execution.

Rules for naming a variable

To write a variable name, all programmers have to follow some pre-defined rules in every programming language. Different types of rules defined in different programming languages. Let’s see rules for naming a variable in PHP programming language:

  • A variable name must be followed by a $ symbol.
  • A variable name must begin with alphabets or ( _ ) underscore symbol.
  • A variable name shouldn’t contain any symbol except underscore.
  • It may be a combination of alphabets and numbers but numbers must not be placed at the starting point.
  • In PHP, a variable name cannot contain spaces.
  • Variables are case sensitive. Therefore, $name and $NAME both are different variables.

Variable example with different cases

Let’s see, each type of example with valid or invalid variable names.

Variable by naming rules
<?php

//variable naming rules

//valid variable names

$ab = "PTP"; 

$_ab = "PHPTREEPOINT";

$a_b = "test";

$ab_ = "tests"; 

$ab9 = 4554;

//Invalid variable names

$9a = 4545;

$@ab = 4455;

$a b = 454;

$-ac = 4788;

When the above code will be executed then the following error will be generated.

Error

PHP Parse error: syntax error, unexpected ‘9’ (T_LNUMBER), expecting variable (T_VARIABLE) or ‘{‘ or ‘$’ in /home/jvu6ZA/prog.php on line 19

Variables with different data types

<?php

//php variables with different data type

$ab = "Php tree point"; //string data type
echo "String data type : " . $ab . "<br>"; //Php tree point

$b = 789; //integer data type
echo "Integer data type : " . $b . "<br>"; //789

$f = 455.45; //Float data type
echo "Floating data type : " . $f . "<br>"; //455.45

Output

String data type : Php tree point
Integer data type : 789
Floating data type : 455.45

Variables with case sensitive

<?php

//php variables case sensitive

$a = 45445; 

$A = 55454545; 

echo $a . "<br>";

echo $A . "<br>";

Output

45445
55454545