PHP Variables Tutorial
last modified February 11, 2025
Variables in PHP are used to store data, such as numbers, strings, or objects. They are essential for dynamic programming, allowing you to manipulate and retrieve data throughout your script. This tutorial covers the basics of PHP variables, including declaration, types, scope, and practical examples.
Declaring Variables
In PHP, variables are declared using the dollar sign $
followed by
the variable name. Variable names must start with a letter or underscore and
cannot start with a number.
<?php $name = "John Doe"; $age = 25; echo "Name: $name, Age: $age";
In this example, $name
stores a string, and $age
stores
an integer. The echo
statement outputs the values.
Variable Types
PHP is a loosely typed language, meaning you don't need to declare the type of a variable. PHP automatically converts variables to the appropriate type based on their usage.
<?php $string = "Hello, World!"; $integer = 42; $float = 3.14; $boolean = true; echo "String: $string, Integer: $integer, Float: $float, Boolean: $boolean";
This example demonstrates different variable types: string, integer, float, and boolean.
Variable Scope
PHP variables have different scopes: local, global, and static. Local variables are accessible only within the function they are declared in, while global variables can be accessed anywhere in the script.
<?php $globalVar = "I am global"; function testFunction() { $localVar = "I am local"; global $globalVar; echo $globalVar . " " . $localVar; } testFunction();
The global
keyword is used to access a global variable inside a
function.
Static Variables
Static variables retain their value between function calls. They are initialized only once and persist throughout the script's execution.
<?php function counter() { static $count = 0; $count++; echo "Count: $count\n"; } counter(); counter(); counter();
Each call to counter
increments the static variable
$count
.
Variable Variables
PHP allows you to use variable variables, where the name of a variable is dynamically set using another variable's value.
<?php $varName = "message"; $$varName = "Hello, Variable Variables!"; echo $message;
Here, $$varName
creates a variable named $message
with
the value "Hello, Variable Variables!".
Constants
Constants are similar to variables but cannot be changed once defined. They are
declared using the define
function.
<?php define("PI", 3.14159); echo "The value of PI is " . PI;
Constants are useful for values that remain unchanged throughout the script.
Superglobals
PHP provides several superglobal variables that are always accessible,
regardless of scope. Examples include $_GET
, $_POST
,
and $_SESSION
.
<?php echo "Your IP address is: " . $_SERVER['REMOTE_ADDR'];
This example retrieves the user's IP address using the $_SERVER
superglobal.
Best Practices for PHP Variables
- Use Descriptive Names: Choose meaningful variable names to improve code readability.
- Avoid Global Variables: Minimize the use of global variables to prevent unintended side effects.
- Initialize Variables: Always initialize variables to avoid undefined behavior.
- Use Constants for Fixed Values: Use constants for values that do not change during script execution.
Source
This tutorial covered the basics of PHP variables, including declaration, types, scope, and practical examples. By mastering variables, you can write more dynamic and efficient PHP scripts.
Author
List all PHP tutorials.