PHP Variables & Scope

Last Updated : May 7, 2021

In this tutorial, we will learn about how to create PHP Variables and their scope. Variables are used to store data information and retrieve them at any point based on the need of the Variables. The data can be a character, string, numbers and, an array of values.

How to Create/Declare PHP Variables?

In PHP, we don’t have to declare the variables first and use them later; they will be created when we initialize and assign specific values. Typical PHP Variables syntax will be $variable_name = value; and = symbol behaves like an assignment operator who assigns the value to specific $variable_name.

$greetings = "Hello World!";

Naming Conventions for PHP Variables

PHP language has some rules for naming the variables. Please find below the major rules,

  • All variable names must start with a $ sign; they will not be considered variables without it.
  • All variable’s names should start with alpha-numeric(a-z, A-Z) characters and underscore(_).
  • A Variable name should not start with numbers or special characters.
  • A Variable name should not contain spaces anywhere in between the names.
  • Variable names are case-sensitive; you should be careful about capital/small letters in them.
  • Variable will hold most recently assigned values among all the assignments.

Please find below the examples for valid/invalid Variable names.

$fruits = "Apple";  // valid
$ fruits = "Apple"; // invalid
$_fruits = "Apple"; // valid
$1fruits = "Apple"; // invalid
$Fruits = "Apple"; // valid
$#@!@!@ = "Apple"; // invalid

Data Types of PHP Variables

PHP language is loosely coupled about defining data types for the Variables. The data types are automatically defined depending upon their assigned values. However, we can declare the Variables with their data type to avoid data mismatch issues and we can type-casting it accordingly. Please find below the examples about automatic data type conversion by PHP.


$sample_variable = "Apple";
echo gettype($sample_variable) . '<br>';

$sample_variable = 42;
echo gettype($sample_variable) . '<br>';

$sample_variable = 3.14159;
echo gettype($sample_variable) . '<br>';

$sample_variable = array(1);
echo gettype($sample_variable) . '<br>';

$sample_variable = NULL;
echo gettype($sample_variable) . '<br>';


Scope of PHP Variables

The score of the variable is defined where and how it can be used in the program (i.e.) In the plain PHP program like one in the above example; the variable $sample_variable can be used anywhere in the program until EOF(End Of File). Whereas the program having Classes, Functions; the variable scope will be different. There are a few variable scopes available in PHP; please find them below.

  • Local Variables
  • Global Variables
  • Static Variables
  • Function Parameters

Please find below the examples of the variable’s scope usage.

$a = 5; // global scope
$x = 2; // global variable

function addition($a, $b) {
  global $x;
  $c = $a + $b + $x; // $b as function parameters
  return $c; // local variable - out of scope apart from this function

$result = addition($a, 4);
echo $result .'<br>'; // output: check it!

// run this code to check the output with it's scope


How to delete Variables in PHP?

PHP does delete the variables automatically based on its scope. However, we can delete the variables manually by using unset built-in function. Please find below the examples,

$greetings = "Welcome to PHP Tutorials";

echo $greetings;
echo $greetings;

How to use Variable as Variable in PHP?

PHP allows us to use Variable as Variable names. This can be used for creating dynamic Variable names and Function names. Please find below the examples,


$a = 'b';
$$a = 'c';
$$$a = 10;
echo $a.'='.$b .'='. $c;


What is Pass by Reference in PHP?

PHP considered a variable’s scope as a local variable inside the function. But to make local variable scope into the global scope, we can pass variables by its reference. So whatever changes those variables have gone through inside the function will be available outside the function scope. Please find below the examples for a better understanding,


$a = 5;

// Pass by Value
function pass_by_value($a) {
  $a = 10;
  return $a;
echo 'Function Processed :: ', pass_by_value($a), ' Original :: ', $a ,'<br>'; 

// Pass by Reference
function pass_by_reference(&$a) {
  $a = 10;
  return $a;
echo 'Function Processed :: ', pass_by_reference($a), ' Original :: ', $a;

Variables are the basic building blocks of the PHP programming language, and in this tutorial series, we will use Variables in many places. Let’s step into the next tutorial about Super Global Variables.