Variables are a fundamental concept in programming, and PHP provides a straightforward way of working with them. In this section, we'll explore how to declare, assign, and use variables in PHP.
Declaring Variables
In PHP, variables are declared using the dollar sign (`$
`) followed by the variable name. The variable name must start with a letter or an underscore (`_`), and it can be followed by any combination of letters, numbers, and underscores.
Here are some valid variable declarations:
$name;
$_age;
$user_id;
$sum2;
Note that variable names in PHP are case-sensitive. So, $name
and $Name
are treated as different variables.
Assigning Values
After declaring a variable, you can assign a value to it using the assignment operator (`=`). PHP is a loosely typed language, which means you don't need to explicitly declare the data type of a variable. The type is determined automatically based on the assigned value.
$name = "Alice"; // String
$age = 25; // Integer
$height = 1.72; // Float (or double)
$isStudent = true; // Boolean
You can also assign values using expressions or other variables:
$x = 10;
$y = 5;
$sum = $x + $y; // $sum will be 15
Variable Scope
The scope of a variable determines where in your code the variable can be accessed and used. PHP has three main variable scopes:
- Local Scope: Variables declared within a function or code block (e.g., loops, conditionals) have local scope and can only be accessed within that function or code block.
- Global Scope: Variables declared outside of any function or code block have global scope and can be accessed from anywhere in the script.
- Static Scope: Variables declared with the `static` keyword within a function maintain their value between function calls
Here's an example demonstrating local and global scope:
$x = 10; // Global variable
function myFunction() {
$y = 20; // Local variable
echo "Inside the function: x = $x, y = $y"; // Outputs: Inside the function: x = 10, y = 20
}
myFunction();
echo "Outside the function: x = $x"; // Outputs: Outside the function: x = 10
// echo "Outside the function: y = $y"; // Error: Undefined variable $y
Variable Variables
PHP also supports variable variables, which means you can use the value of one variable as the name of another variable. This feature can be useful in certain situations, such as when working with arrays or dynamic data.
$x = "name";
$$x = "Alice"; // Equivalent to $name = "Alice";
echo $name; // Outputs: Alice
While variable variables can be handy, they should be used judiciously, as they can make your code harder to read and maintain.
Constants
In addition to variables, PHP also supports constants, which are immutable values. Constants are defined using the `define()` function or the `const` keyword (as of PHP 5.3).
define("PI", 3.14159);
// or
const PI = 3.14159;
echo PI; // Outputs: 3.14159
Constants are commonly used to store values that should remain unchanged throughout the execution of a script, such as configuration settings or mathematical constants.
In the next section, we'll explore the different data types supported by PHP and how to work with them.