Variables are a means of holding data. Variables are used to store data temporarily in the memory of any application. Temporarily means that you can change this data anytime you want. Since the data of the variable can be changed, its name is Variables — that is, whose data or value varies or changes.
In PHP programming, variables allow you to work with different types of data like strings, numbers, arrays, objects, etc.
Basic Rules for PHP Variables:
- All variables start with a dollar sign ($) followed by the variable name.
- Example: $name, $age, $userScore
- Variable names must start with a letter or underscore (_), but cannot start with a number.
- Valid: $userName, $_age
- Invalid: $1name
- Variable names can contain letters, numbers, and underscores but cannot contain spaces.
- Example: $user_name is valid, but $user name is not.
- PHP variable names are case-sensitive, so $name and $Name are different variables.
Declaring and Using Variables
Variables are created automatically when you assign a value to them using the assignment operator (=). You do not need to declare them explicitly like in some other programming languages.
Example:
$name = "John"; // String
$age = 25; // Integer
$height = 1.75; // Float
$is_student = true; // Boolean
In this example:
- $name is a string that stores “John”.
- $age is an integer that stores 25.
- $height is a float that stores 1.75.
- $is_student is a boolean that stores true.
Variable Types in PHP
PHP is a loosely typed language, meaning you don’t need to explicitly specify the type of the variable. The type is determined by the value assigned to the variable. Here are some common types:
- String: A sequence of characters (text).
- Example: $name = “Alice”;
- Integer: A whole number.
- Example: $age = 25;
- Float (or Double): A number with a decimal point.
- Example: $price = 10.99;
- Boolean: Represents a value of true or false.
- Example: $is_admin = true;
- Array: A collection of values.
- Example: $colors = array(“Red”, “Green”, “Blue”);
- Object: An instance of a class, used in object-oriented programming.
- NULL: A variable with no value.
- Example: $x = NULL;
String Variables
Strings in PHP are sequences of characters enclosed in single (‘) or double (“) quotes.
Example:
In this example, the double quotes allow variable interpolation (the $firstName and $lastName variables are parsed inside the string).
<?php
$firstname = "John";
$lastname = "Doe";
echo "Hello, $firstName $lastName";
?>
Concatenation
To combine two or more strings, PHP uses the dot (.) operator.
<?php
$greeting = "Hello, " . $firstName . " " . $lastName;
echo $greeting;
?>
Integer and Float Variables
You can store whole numbers or floating-point numbers (numbers with decimals) in PHP variables.
<?php
$x = 10; // Integer
$y = 5.5; // Float
echo $x + $y;
?>
Boolean Variables
A boolean variable can either be true or false.
<?php
$is_active = true;
$is_logged_in = false;
if ($is_active) {
echo "User is active";
}
?>
Arrays in PHP
Arrays allow you to store multiple values in a single variable.
Arrays in PHP can be indexed or associative:
- Indexed Array: Uses numeric keys.
- Example: $fruits[0] = “Apple”;
- Example: $fruits[1] = “Banana”;
<?php
$fruits = array("Apple", "Banana", "Cherry");
echo $fruits[0];
?>
- Associative Array: Uses named keys.
<?php
$person = array("name" => "John", "age" => 30);
echo $person['name'];
?>
Superglobal Variables
PHP provides several superglobal variables that are accessible globally throughout the script, regardless of scope. These are predefined variables that store information like user input, session data, and server environment details.
Some common superglobal variables include:
- $_GET : Stores data sent via HTTP GET method.
- $_POST : Stores data sent via HTTP POST method.
- $_SERVER : Contains information about headers, paths, and script locations.
- $_SESSION : Stores session variables for the current user.
- $_COOKIE : Stores cookies sent by the browser.
Example using $_POST:
<form method="POST" action="process.php">
Name: <input type="text" name="name">
<input type="submit" value="submit">
</form>
process.php
<?php
$name = $_POST['name'];
echo "Hello, " . $name;
?>
Variable Scope
PHP has three types of variable scope:
- Local Scope: Variables declared inside a function are only accessible within that function.
- Global Scope: Variables declared outside of a function can be accessed anywhere in the script, except within functions (unless declared global).
<?php
$x = 10; // Global variable
function myFunction() {
global $x; // Access global variable inside a function
echo $x;
}
myFunction();
?>
3. Static Scope: A static variable retains its value between function calls.
<?php
function countVisits() {
static $count = 0;
$count++;
echo $count;
}
countVisits();
countVisits();
?>
Variables are essential for every programming language, and for PHP also. With variables, you can store and manipulate data efficiently throughout your code. By using variables work, their scope, and the different types of variables in PHP will enable you to build more dynamic and interactive applications.