PHP Variables – Step By Step Guide

If you want to store data in temporary storage and access them anywhere in your PHP script then you need to learn PHP variables.

In this article, we’ll explain the fundamentals of PHP variables, including how to declare, assign, and use them effectively.

What is PHP Variables

In PHP, a variable is just like a container that is used to store temporary values. It can store various data types like s numbers, strings, and more complex data structures.

Variables are essential for storing and manipulating data dynamically with user input.

Syntax –

$variableName = Value;
  • $ Doller symbol for a variable.
  • variableName: The name of the variable.
  • = Assignment Operator to assign a value to the variable.
  • Value: The value being assigned.
  • ; Semicolon Marks the end of the statement.

Example –

$tutorial = "PHP Tutorial";

Variable Naming Rules 

  • A Variable name must start with a dollar sign ($).
  • Variable names are case-sensitive. This means that variables with similar names but different cases, such as uppercase, lowercase, and camel case, or a combination of cases, are considered different from each other.
  • Variable names can contain letters, numbers, and underscores, but they must start with a letter or an underscore.

Valid variables –


Invalid Variable –

  • Variable names cannot contain spaces or special characters (except underscores).

Valid variable –


Invalid variables –

$coding tutorial

Declaring Variables

  • To declare  a variable in PHP, You must add a dollar $ symbol at the beginning of the variable name, also assign a value to it using the assignment operator = and end the variable using a semicolon(;)
$tutorial = "PHP Tutorial";
$totalTopics = 30;

Accessing Variables

You can access variables in PHP for various operations, such as displaying values, manipulating data, and more.

  • You can display variables using echo, print
$tutorial = "PHP Tutorial"; 
$totalTopics = 30;

echo $tutorial;
echo $totorialTopics;
  • You can assign a variable to another variable –
$num1 = 50;
$num2 = num1;

echo $num2;
  • You can add variables
$num1 = 40;
$num2 = 50;

$sum = $num1 + $num2;

echo $sum;

Variable Data Type

PHP is a loosely typed language, which means that you need not declare a variable with data types, PHP automatically can identify the data type from its value.

$tutorial = "PHP Tutorial";
$totalTopics = 50;

PHP will identify $tutorial has string type data and $tutorialTopics has integer type data.


I hope you’ve learned how to declare, assign values to, and use variables in PHP. Now, you can create some variables and practice using them yourself.

As you continue to explore PHP and delve into more topics, you will discover additional ways to utilize variables and expand your understanding of them.”