What is Variable in PHP

Variables are used to store data/information like string of text, numbers, etc.

PHP variable does not declared before adding a value to it. PHP automatically converts the variable to the correct data type depending on it's value.

Declaring a variable it can be reused the code.

Variables are assigned with the ( = ) operator and the variable on the left side and the expression to be evaluated on the right.

All variables in PHP are denoted with a leading the dollar sign ( $ ).

Create PHP Variable

Syntax :

<?php
    $variable_name = value;
?>

Example :

The following example with output the sum of two variables :

<?php
$a = 5;
$b = 10;
echo $a + $b;
?>

Variable names in PHP are case-sensitive it means $colour is not same as $CLOUR.

<?php
    $colour = "Red";
    echo "My favorite colour is $str";
    // below statement will give error
    echo "My favorite colour is $STR";
?>

Output Variable

My favorite colour is Red

Notice: Undefined variable: STR