PHP Variable
A variable in PHP is used to store data that can be manipulated or retrieved later. PHP variables are flexible and easy to use, making them a key part of any PHP script.
What Is a Variable in PHP?
A variable is a symbolic name for a value.
In PHP, variables are dynamically typed, meaning they can store any data type (e.g., string, integer, array).
Rules for Declaring Variables
Variables in PHP always start with a $
symbol.
The variable name must:
Start with a letter or an underscore _
.
Contain only letters, numbers, or underscores.
Variable names are case-sensitive ($name
and $Name
are different).
Declaring a Variable
Use the $
symbol followed by a name to declare a variable.
Assign a value using the assignment operator (=
).
Example:
<?php
$greeting = "Hello, World!"; // String variable
$age = 25; // Integer variable
$is_logged_in = true; // Boolean variable
echo $greeting; // Outputs: Hello, World!
?>
Variable Data Types
PHP automatically detects the data type of a variable. Common data types include:
- String: Text (e.g.,
"Hello"
) - Integer: Whole numbers (e.g.,
42
) - Float: Decimal numbers (e.g.,
3.14
) - Boolean:
true
orfalse
- Array: A collection of values
- Object: Instance of a class
- NULL: No value
Example:
<?php
$name = "Alice"; // String
$age = 30; // Integer
$height = 5.7; // Float
$is_student = false; // Boolean
$courses = ["Math", "Science"]; // Array
?>
Variable Scope
The scope determines where a variable can be accessed. PHP has three types of variable scope:
Local Scope: Accessible only within a function.
Global Scope: Accessible everywhere, except inside functions (unless declared global).
Static Variables: Retain their value between function calls.
Example:
<?php
// Global scope
$name = "John";
function displayName() {
// Access global variable inside a function
global $name;
echo $name;
}
displayName(); // Outputs: John
?>
Variable Variables
You can use the value of one variable as the name of another variable.
Example:
<?php
// Declare a variable
$name = "John Doe"; // String
$age = 25; // Integer
$isStudent = true; // Boolean
// Use the variables
echo "Name: " . $name . "\n";
echo "Age: " . $age . "\n";
echo "Is Student: " . ($isStudent ? "Yes" : "No");
?>
Special Variables in PHP
$_POST
: Used to collect form data sent via HTTP POST.
$_GET
: Used to collect form data sent via HTTP GET.
$_SERVER
: Contains information about server and execution environment.
Example: Working with PHP Variables
<?php
// Declare variables
$first_name = "Alice";
$last_name = "Johnson";
$age = 28;
// Concatenate strings
$full_name = $first_name . " " . $last_name;
// Output the values
echo "Name: " . $full_name . "<br>";
echo "Age: " . $age . "<br>";
// Arithmetic operation
$next_year_age = $age + 1;
echo "Next year, you will be " . $next_year_age . " years old.";
?>
Best Practices for PHP Variables
Use Descriptive Names
Use Descriptive Names: Make your variable names meaningful.
$user_age = 25; // Clear and descriptive
Avoid Overwriting Variables
Avoid Overwriting Variables: Use unique names to avoid confusion.
Use Proper Case
Use Proper Case: Use camelCase or snake_case consistently.