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:

Tutorials Class - Output Window

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:

Tutorials Class - Output Window

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. $name and $Name are different.
  • Do not use spaces or special characters like - or @.
Variable nameValid?
$nameYes
$_nameYes
$user_name2Yes
$2nameNo, starts with a digit
$first-nameNo, hyphen is not allowed
$first nameNo, 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:

Tutorials Class - Output Window

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:

Tutorials Class - Output Window

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) or gettype($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: $$name uses 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:

Tutorials Class - Output Window

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 global or parameters to share values.

Congratulations! Chapter Finished. Learn more about the similar topics:
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
Interview Questions & Answers
How to check if a variable is empty in PHP?
Is PHP a case sensitive language?
What are the different data types in PHP?
How to define a Constant in PHP?
What is the difference between $var and $$var in PHP?
Which special characters are allowed in PHP variable names?
Can we print multiple variables in a single echo statement in PHP?