In PHP, variables are used to store data that can be manipulated or accessed later. A variable in PHP is a container for a value, and each variable has a unique name that can be referenced in your code.
Here is a detailed guide to PHP variables:
1. Declaring Variables
A variable in PHP starts with a dollar sign ($
) followed by the variable name. The name of the variable must start with a letter or an underscore (_
) and can contain letters, numbers, and underscores.
Basic Example:
In this example:
$name
is a variable storing the string"John"
.$age
is a variable storing the integer30
.$isActive
is a variable storing a boolean value?true
.
2. Variable Naming Rules
- The name must start with a letter or underscore (
_
). - The name can contain letters, numbers, and underscores, but cannot start with a number.
- Variable names in PHP are case-sensitive. For example,
$name
and$Name
are two different variables.
Valid variable names:
Invalid variable names:
3. Data Types in PHP
PHP is a loosely typed language, which means you don't have to specify the type of data a variable will hold. The type is inferred based on the value assigned to the variable.
Common data types in PHP:
- String: A sequence of characters enclosed in quotes.
- Integer: A whole number (positive or negative).
- Float (Double): A number with a decimal point.
- Boolean: A value of either
true
orfalse
. - Array: A collection of values.
- Object: A class instance.
- NULL: A special variable representing no value.
Example:
4. Variable Scope
Variables in PHP have scopes, which determine where they can be accessed in your program.
a) Global Scope:
- Variables declared outside of functions are in the global scope.
- These variables can be accessed anywhere outside functions but cannot be accessed directly inside functions.
Example:
To access global variables inside functions, you must use the global
keyword or the $GLOBALS
array.
b) Local Scope:
- Variables declared inside a function are local to that function and cannot be accessed outside of it.
Example:
c) Static Variables:
- Static variables are local variables that retain their value even after the function call ends. You can use the
static
keyword to make a variable static inside a function.
Example:
5. Variable Variables
PHP allows variable variables, which means the name of a variable can be stored in another variable. This can be useful when dynamically creating variable names.
Example:
In this example, the value of $varName
("greeting") is used as the name of another variable, $$varName
, which is equivalent to $greeting
.
6. Superglobals
PHP has some predefined global variables that are always accessible, regardless of scope. These are called superglobals. Common superglobals include:
$_GET
: Used to collect form data sent via HTTP GET.$_POST
: Used to collect form data sent via HTTP POST.$_SESSION
: Used to store session variables.$_COOKIE
: Used to access cookies.$_SERVER
: Contains information about the server environment.$_FILES
: Used to handle file uploads.$GLOBALS
: Allows access to global variables.
Example:
7. Unset Variables
You can destroy a variable using the unset()
function. This removes the variable from memory and makes it unavailable.
8. References to Variables
PHP allows you to create references to variables using the reference operator (&
). This means both variables will point to the same memory location, so modifying one will also affect the other.
Conclusion
- PHP variables are denoted by a dollar sign (
$
), followed by the variable name. - PHP is loosely typed, meaning variables do not require explicit type declaration.
- Variable scope determines where variables can be accessed in the code (local, global, or static).
- PHP supports superglobals like
$_GET
,$_POST
, etc., which are always available. - You can unset variables to remove them from memory.
- References allow two variables to point to the same data.
Understanding variables in PHP is crucial, as they are the foundation of manipulating and storing data within your applications.