PHP variable is declared using a $ sign followed by the variable name.
As PHP is a loosely typed language, so you do not need to declare the data types of the variables. PHP automatically converts the variable to its correct data type.
Syntax
$variablename=value;
The value of a variable is the value of its most recent assignment.
Rules for declaring PHP variable:
- A variable must start with a dollar ($) sign, followed by the variable name.
- A variable name must start with a letter or underscore (_) character. It cannot start with a number or special symbols.
- It can only contain alpha-numeric character and underscore (A-z, 0-9, _).
- A PHP variable name cannot contain spaces.
- PHP variables are case-sensitive, so $var and $VAR both are treated as different variable.
PHP Variable Example
<?php
$str="hello string";
$x=2893;
$y=443.6;
echo "string is: $str <br/>";
echo "integer is: $x <br/>";
echo "float is: $y <br/>";
?>