PHP Variable
In PHP, a variable is declared using a $
sign followed by the variable name. Here are some important points to know about variables:
- As PHP is a loosely typed language, we do not need to declare the data types of the variables. It automatically analyzes the values and makes conversions to its correct datatype.
- After declaring a variable, it can be reused throughout the code.
- Assignment Operator (
=
) is used to assign the value to a variable.
Syntax of declaring a variable in PHP:
$variablename = value;
Rules for Declaring PHP Variable
- A variable must start with a dollar (
$
) sign, followed by the variable name. - It can only contain alphanumeric characters and underscores (
A-z, 0-9, _
). - A variable name must start with a letter or underscore (
_
) character. - A PHP variable name cannot contain spaces.
- Variable names cannot start with a number or special symbols.
- PHP variables are case-sensitive, so
$name
and$NAME
are treated as different variables.
PHP Variable: Declaring String, Integer, and Float
<?php
$str = "hello string";
$x = 200;
$y = 44.6;
echo "String is: $str <br/>";
echo "Integer is: $x <br/>";
echo "Float is: $y <br/>";
?>
PHP Variable: Sum of Two Variables
<?php
$x = 5;
$y = 6;
$z = $x + $y;
echo $z;
?>
PHP Variable: Case Sensitivity
In PHP, variable names are case-sensitive. So variable name $color
is different from $Color
, $COLOR
, $COLor
, etc.
<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
Variable Scope
- Global Scope: Accessible throughout the script unless overridden.
- Local Scope: Declared within a function and accessible only within that function.
- Static Variables: Retain their value between function calls using the
static
keyword. - Superglobals: Predefined variables like
$_GET
,$_POST
,$_SESSION
, etc., are accessible globally.
Example of Scope
<?php
$x = 10; // Global scope
function test() {
$y = 20; // Local scope
global $x; // Access global variable
echo $x + $y;
}
test();
?>
Local Variable
The variables that are declared within a function are called local variables for that function. These local variables have their scope only in that particular function in which they are declared. They cannot be accessed outside the function.
<?php
function local_var() {
$num = 45; // local variable
echo "Local variable declared inside the function is: " . $num;
}
local_var();
?>
Global Variable
The global variables are the variables that are declared outside the function. These variables can be accessed anywhere in the program. To access a global variable within a function, use the global
keyword or the $GLOBALS
array.
<?php
$name = "Sanaya Sharma"; // Global Variable
function global_var() {
global $name;
echo "Variable inside the function: " . $name;
echo "<br>";
}
global_var();
echo "Variable outside the function: " . $name;
?>
Using $GLOBALS Instead of Global
<?php
$num1 = 5; // global variable
$num2 = 13; // global variable
function global_var() {
$sum = $GLOBALS['num1'] + $GLOBALS['num2'];
echo "Sum of global variables is: " . $sum;
}
global_var();
?>