PHP Variables

Definition of PHP Variables

A PHP variables name cannot start with a number. A variable name can only contain alpha–numeric characters and underscores (A-z, 0-9, and _ )

First of all Variable names are case-sensitive ($male and $MALE are two different variables)

  • We know that very well, variables always stored the data.
  • A variable is a temporary storage that is used to store data temporarily.
  • In PHP, a variable is declared using $ sign followed by a variable name. i.e. $country.

Example:

$variablename=value;

PHP Variable: Declaring string, integer, and float
the example to store string, integer and float values in PHP variables.

File: example1.php

<?php 
$str="Welcome"; 
$x=500; 
$y=77.6; 
echo "string is: $str <br/>"; 
echo "integer is: $x <br/>"; 
echo "float is: $y <br/>"; 
?>

Output:

string is: Welcome
integer is: 500
float is: 77.6

PHP Variable: Sum of two variables
File: example2.php

<?php 
$x=25; 
$y=61; 
$z=$x+$y; 
echo $z; 
?>

Output:

86

PHP Variable: case sensitive
therefore,  variable names are case sensitive. So variable name “color” is different from Color, COLOR, COLor etc.

File: example3.php

<?php 
$color="red"; 
echo "My Bike is " . $color . "<br>"; 
echo "My Car is " . $COLOR . "<br>"; 
echo "My House is " . $coLOR . "<br>"; 
?>

Note:

  • finally, The PHP variables start with the $ sign, (i.e. $Car) followed by the name of the variable and In PHP name always must start with a letter or the underscore character only but the name can not start with a number.

Resource
PHP.Net