PHP variables start with $ and are created when you assign a value. Use clear names, initialize values before use, and keep input validation separate from variable declaration.
$name = 'Ravi';
$age = 24;
$isMember = true;
if ($isMember) {
echo htmlspecialchars($name, ENT_QUOTES, 'UTF-8') . ' is a member.';
}
This is the recommended starting pattern. The detailed examples below are retained for explanation, comparison and related variations.
Variables are containers to store data. We can add or change or delete data from the variable and use them in our script. Here are some basic rules to create variables.
$var1='testing'; // correct
var1='testing'; // Wrong - error
_var1='testing'; // wrong -error
5var='testing'; // wrong -error
Variable should contain numbers, alphabet and underscore ( A-z, 0-9 AND _ )
$var1='testing'; // correct
$va!3='testing'; // wrong -error
$var^4='testing';// wrong -error
After $ we can't use number, it must be alphabet or underscore
$_var1='testing'; // correct
$1var='testing'; // wrong error
$var1='testing'; // correct
Variables are case sensitive gettype() to check the variable type.
<?php
$my_var=true;
echo gettype($my_var); // boolean
$my_var=5;
echo gettype($my_var); // integer
$my_var=5.6;
echo gettype($my_var); // bouble
$my_var='plus2net.com';
echo gettype($my_var); // string
$my_var=array('Mango','Banana','Apple');
echo gettype($my_var); // array
$my_var=Null;
echo gettype($my_var); // NULL
?>
PHP variables can take any type of data and change their type within the page also. ( in above example $my_var changes with type of data ) . It depends on its most recent assignment.
<?php
error_reporting(E_ALL);
echo $name1;
?>
Output
PHP Notice: Undefined variable: name1 in D:\php_files\test.php on line 4
Before using the variable, we can check if it is declared or not by using is_set() or by using empty().
<?php
error_reporting(E_ALL);
if(isset($name1)){
echo $name1;
}else{
echo " Variabe is not declared ";
}
?>
Output
Variabe is not declared
We can unset a variable.
<?php
$v1=4;
function test(){
$v1=10;
echo "Value inside the function:".$v1;
}
test();
echo "<br>Value outside the function:".$v1;
?>
Output
Value inside the function:10
Value outside the function:4
$GLOBALS holds the name of the variables as key and content as value of the variable. Once the variable is declared as element of $GLOBAL variable, then it is available throughout the script (including inside the functions).
$v1=4;
$v2=3;
function test(){
$GLOBALS[v1]=10;
$GLOBALS[v2]=20;
$GLOBALS[v3]=$GLOBALS[v1]+$GLOBALS[v2];
}
test();
echo $v3; // Output is 30
echo $v2; // Output is 20
More on Scope of variables in PHP functions
<?php
function my_function(){
static $my_static_variable=0;
$my_local_variable=0;
$my_static_variable=$my_static_variable + 1 ;
$my_local_variable=$my_local_variable + 1 ;
echo "Static variable :".$my_static_variable;
echo ",Local Variable :".$my_local_variable;
}
my_function();
echo "<br>";
my_function();
?>
Output is here. See how static variable value is retained after execution of first call of the function whereas the local variable is destroyed.
Static variable :1, Local Variable :1
Static variable :2, Local Variable :1
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.