PHP Variables
A variable is a name that stores a value in memory. The value can change while the script runs. In PHP every variable starts with the dollar sign ($).
Create a variable
You do not need to declare a variable or its data type. PHP creates it when you assign a value using the assignment operator (=), and it picks the data type from the value.
<?php
$name = "Robin"; // string
$age = 25; // integer
$price = 99.50; // float
$isActive = true; // boolean
echo "Name: $name<br>";
echo "Age: " . $age . "<br>";
echo "Price: $price<br>";
var_dump($isActive);
?>
Output:
Name: Robin
Age: 25
Price: 99.5
bool(true)
A variable can change its type later, because PHP is a loosely typed language.
<?php
$x = 10;
$x = "Hello";
echo $x;
?>
Output:
Hello
Rules for naming variables
- A variable name starts with
$, then a letter or an underscore. It can not start with a digit. - The rest of the name can have letters, digits and underscores only.
- Variable names are case-sensitive.
$nameand$Nameare different. - Do not use spaces or special characters like
-or@.
| Variable name | Valid? |
|---|---|
| $name | Yes |
| $_name | Yes |
| $user_name2 | Yes |
| $2name | No, starts with a digit |
| $first-name | No, hyphen is not allowed |
| $first name | No, space is not allowed |
Use clear names like $total_price or $totalPrice and keep the same style in the whole project. See also which special characters are allowed.
Variable scope
Scope means where a variable can be used. A variable created outside a function is a global variable, and it is not visible inside functions by default.
<?php
$x = 5;
function test() {
echo isset($x) ? "Visible" : "Not visible";
}
test();
?>
Output:
Not visible
To use a global variable inside a function, write the global keyword, or better, pass it as a parameter.
<?php
$x = 5;
function show() {
global $x;
echo $x;
}
show();
?>
Output:
5
A variable created inside a function is local. It exists only while the function runs. Read more about functions in PHP Functions.
Useful things to know
- Check the type: use
var_dump($x)orgettype($x). See how to check the data type. - Remove a variable:
unset($x)deletes it. - Check if it exists: use
isset(). Learn it in PHP isset(). - Variable variables:
$$nameuses the value of $name as a variable name. See $var vs $$var. - Constants are like variables, but their value can not change. See PHP Constants.
<?php
$a = 1;
$b = &$a; // $b is a reference to $a
$b = 2;
echo $a;
?>
Output:
2
The last example uses a reference (&). Both names point to the same value, so changing $b also changes $a.
Summary
- Variables start with
$and need no declaration. - Names are case-sensitive and can not start with a digit.
- PHP decides the data type automatically.
- A variable inside a function is local, use
globalor parameters to share values.
| Exercises & Assignments |
|---|
| Write a program to print “Hello PHP” using variable |
| Write a program to print a string using echo+variable. |
| Write a program to print two variables in single echo |