PHP Variables
PHP Variables are one of the most fundamental concepts in programming. It is used to store data that can be accessed and manipulated within your code. Variables in PHP are easy to use, dynamically typed (meaning that you do not need to declare their type explicitly), and essential for creating dynamic, interactive web applications.
Declaring Variables in PHP
To declare a variable in PHP, you simply assign a value to it using the $ symbol followed by the variable name. PHP variables are case-sensitive and must start with a letter or an underscore, followed by any number of letters, numbers, or underscores.
Example
<?php
$name = "XYZ"; // String
$age = 30; // Integer
$salary = 45000.50; // Float
$isEmployed = true; // Boolean
?>
Variable Naming Conventions
In PHP, it’s important to follow certain naming conventions for PHP variables to ensure readability and maintainability:
- Start with a Letter or Underscore: Variable names must begin with a letter or an underscore (_), not a number.
- Use Descriptive Names: Variable names should be descriptive of their purpose, e.g., $userName, $totalAmount.
- Case Sensitivity: PHP variable names are case-sensitive, meaning $name and $Name are different variables.
- Avoid Reserved Words: Do not use PHP reserved words or keywords as variable names (e.g., function, class, echo).
Example of Valid and Invalid Variable Names
<?php
$firstName = "Alice"; // Valid
$_age = 25; // Valid
$2ndPlace = "Bob"; // Invalid: Cannot start with a number
$class = "Physics"; // Valid, but avoid reserved words
?>
Data Types in PHP
- Integers: Integers are whole numbers without decimals, which can be positive or negative.
- Doubles: Floats are numbers with decimal points or in exponential form.
- NULL: The NULL data type represents a variable with no value.
- Strings: Strings are sequences of characters, enclosed in single (‘ ‘) or double (” “) quotes.
- Booleans: Booleans represent two possible values: true or false.
- Arrays: Arrays are used to store multiple values in a single variable. Arrays can be indexed, associative, or multidimensional.
- Objects: Objects are instances of classes, which are templates for creating data structures with properties and methods.
- Resources: Resources are special variables that hold references to external resources, such as database connections or file handles.
PHP Variable Scope
The scope of a variable refers to where it can be accessed within the code. PHP variables can have local, global, static, or superglobal scope.
1. Local Scope or Local Variable
Variables declared within a function have local scope and cannot be accessed outside the function. Any declaration of a variable outside the function with the same name (as within the function) is a completely different variable.
Example: This example shows the local variable in PHP.
<?php
$num = 60;
function local_var() {
// This $num is local to this function
// The variable $num outside the function
// is a completely different
$num = 50;
echo "Variable num inside function is: $num \n";
}
local_var();
// The $num outside function is a completely
// different from inside local_var()
echo "Variable num outside function is: $num";
?>
Output
Variable num inside function is: 50 Variable num outside function is: 60
2. Global Scope or Global Variable
The variables declared outside a function are called global variables. These variables can be accessed directly outside a function. To get access within a function we need to use the “global” keyword before the variable to refer to the global variable.
Example: This example shows the Global Variables in PHP.
<?php
$num = 20;
// Function to demonstrate use of global variable
function global_var() {
// We have to use global keyword before
// the variable $num to access within
// the function
global $num;
echo "Variable num inside function: $num \n";
}
global_var();
echo "Variable num outside function: $num \n";
?>
Output
Variable num inside function: 20 Variable num outside function: 20
3. Static Variables
It is the characteristic of PHP to delete the variable. Once it completes its execution and the memory is freed. But sometimes we need to store the variables even after the completion of function execution. To do this, we use the static keywords and the variables are then called static variables. PHP associates a data type depending on the value for the variable.
Example: This example shows the Static variable in PHP.
<?php
// Function to demonstrate static variables
function static_var() {
// Static Variable
static $num = 5;
$sum = 2;
$sum++;
$num++;
echo $num, "\n";
echo $sum, "\n";
}
// First function call
static_var();
// Second function call
static_var();
?>
Output
6 3 7 3
Note: You must have noticed that $num regularly increments even after the first function call but $sum doesn’t. This is because $sum is not static, and its memory is freed after the execution of the first function call.
4. Superglobals
Superglobals are built-in arrays in PHP that are accessible from anywhere in the script, including within functions. Common superglobals include $_GET, $_POST, $_SESSION, $_COOKIE, $_SERVER, and $_GLOBALS.
<?php
// Using $_SERVER superglobal to
// get server information
echo $_SERVER['SERVER_NAME'];
?>
Variable Variables
- PHP allows us to use dynamic variable names, called variable variables.
- Variable variables are simply variables whose names are dynamically created by another variable’s value.
Example: This example shows the declaration of a variable by the use of the another variable.
<?php
$a = 'hello'; //hello is value of variable $a
$$a = 'World'; //$($a) is equals to $(hello)
echo $hello; //$hello is World i.e. $hello is new variable with value 'World'
?>
Output
World