PHP Variables (Complete Guide with Examples)

In this tutorial, you will see complete information about PHP variables. Their definition, usage, scope, examples, and more.

Let’s start with the definition.

What are Variables in PHP?

The variables in PHP are considered like containers that stores data. The concept of variables is the same in all programming languages but the way of using them is different.

PHP variables are used for storing data like numbers, strings, and, arrays, etc. Variables are used almost everywhere in a program, from top to bottom.

Now let’s see how to use variables.

Tip: See how to run PHP code on your computer if you don’t know how to run PHP on your computer.

How to Declare Variables in PHP?

In other programming languages, we first declare variables and then use them separately. But in PHP, when we create a variable it declares by itself. So, we don’t need to declare first and then use it.

Because PHP is a loosely typed language. So, we don’t need any declaration.

All the variables in PHP start with a $ sign followed by the name of the variable.

So let’s see an example of using PHP variables.

Example

In the code given below,

  • every variable is starting with a $ sign.
  • The Fname, Lname, and Age are variable names.
  • John‘, ‘Doe‘, and ‘20‘ are the values of variables. (Also we can say, this is the data that variables store)
  • =‘ is equal to sign is an assignment operator.
→ index.php
<?php
$Fname = "John";
$Lname = "Doe";
$Age = 20;
?>

Things to Remember

Just remember these simple things while working with variables.

  • When you assign a text value to a variable, you will always use double quotes around the value.
  • Don’t use quotes around numbers.
  • Must use a semi-colon; at the end of every statement. It is crucial.

This is how you can use variables in PHP.

What if you want to display the value of these variables on the screen? Let’s see.

How to Display Variable Value in PHP?

It’s very simple, we use an echo statement to display an output of a variable in PHP.

To do this, write the echo statement with the variable name.

Example

The below code is showing two ways to output variables.

You can display the value of a variable by just typing each echo statement separately on each line. Or you can write all the variables on a single line with dot signs to make it more productive.

→ index.php
<?php
$Fname = "John";
$Lname = "Doe";
$Age = 20;
 
echo $Fname .  "<br>";
echo $Lname . "<br>";
echo $Age . "<br>";
 
//To Make it more productive. You can write it on one line.
//method 1
echo "My Name is "  .   $Fname   .   "."    .     $Lname   .    " And I am "  .   $Age;
//method 2
echo "My Name is $Fname $Lname. And I am $Age";
 
?>

Note: The dots are necessary if you want to use variables together in a single statement.

I used <br> tag for the new line. Otherwise, all the variables will output on the same line.

The output of the above code will look like this:

→ Code Output
John
Doe
20
My Name is John Doe. And I am 20
My Name is John Doe. And I am 20.

With this, we have a few PHP variable naming rules that you should have in mind.

PHP Variable Naming Rules

Always keep these four variable naming rules in mind while creating a variable in PHP.

  • All the variables must start with a $ sign.
  • PHP variables are case-sensitive. Mean $Name and $name are two different variables.
  • Variable names can start with a ‘_‘ underscore character or a letter. But you can’t start a variable name with a number.
  • You can only use characters, numbers and ‘_’ underscores in variable names. (A-Z, 0-9, and _).

PHP Variable Scope

There are three different variable scopes in PHP.

  • Global Scope
  • Local Scope
  • Static Scope

Difference between Global Scope and Local Scope Variables in PHP

It’s very easy to understand the difference between global and local scope variables.

Global Scope Variables

When we declare a variable outside of function then it is called a global scope variable. And we can’t access global scope variables in any function.

For Example, in the code given below, the $name variable is declared outside of the test() function.

→ index.php
<?php

$name = "john"; //global scope variable
 
//test function
function test(){
    echo "My name is $name";
}
 
//calling function
test();

?>

In the below output, you’re seeing an error because there is no variable inside a function. This is telling us that there is an error on line 6.

→ Code Output
My name is PHP Notice: Undefined variable: name in /home/cg/root/3326961/main.php on line 6

Note: You will learn more about PHP functions in our later tutorials in detail.

Local Scope Variables

Similarly, when we declare a variable inside a function then it is called a local scope variable of that function. And we can’t use and access this variable outside of that function.

For Example, in the code given below, the same $name variable is declared inside the test() function. So, when we call a function, it outputs a value on the screen. It is ignoring the $name variable outside of the function.

→ index.php
<?php

$name = "Jack";//global scope variable
 
//test function
function test(){
    $name = "John";//local scope variable
    echo "My name is $name";
}
 
//calling function
test();

?>

In the output below, when we can function it shows output without any error. Because the local scope variable inside a function is specified.

→ Code Output
My name is John

Static Scope Variables

Static is basically a keyword in PHP. We use it to declare those types of variables that we can reuse inside the function. Because when functions are executed, the variables are deleted by themselves. That means we can’t use those local scope variables again.

So, we declare variables with a static keyword inside the function.

For example, in the code below, I’m calling the test() function 5 times. Each time when it output the value, it is different from the previous one. Because it remembers the previous function call.

→ style.css
<?php
function test() {
  static $x = 0; //static variable
  echo $x . "<br>";
  $x = $x + 3;
}
 
//calling function 5 times
test();
test();
test();
test();
test();
 
?>

The output will be:

→ Code Output
0
3
6
9
12

PHP Variables Date Types

Data types are the types of data that a variable can store like numbers, strings, etc.

In PHP fortunately, there are not any specific keywords for PHP data types. That means when you create a variable in PHP, you can store any type of data in variables. The PHP automatically figures out which type of data a variable holds.

But it does have some limitations.

Here are 8 types of data that you can store in variables.

Most Used Data Types in PHP

  • Integers – (integers are whole numbers without decimal points e.g. 42,334,243,5)
  • Doubles – (these are numbers with decimal points e.g. 3.44, 342.3, etc)
  • NULL – (it has only one value)
  • Booleans – (boolean is a variable data type that has only two values false and true)
  • Strings – (in simple words, it is a text that we write with double quotes around it)
  • Arrays – (this is a collection of multiple data types).

You will see more about PHP variable types in our later tutorials in detail.

If you want you can see more about data types in detail.

Also, read this advanced PHP tutorial to connect your HTML Form to MySQL database using PHP.

That’s it. Hope this guide will be helpful. If you have any questions related to this guide then feel free to ask in the comment section below. Also, share and subscribe.