Now that your local server is ready, it’s time to learn one of the most fundamental building blocks of programming: PHP Variables. Every dynamic website you build will rely heavily on storing and manipulating data, and that all starts with understanding PHP Variables properly.

This is Tutorial 2 of 10 in our PHP Basics for Beginners series. In this tutorial, you will learn how to declare PHP Variables, understand naming rules, and explore the different data types PHP supports.

What are PHP Variables?

PHP Variables are containers used to store data such as numbers, text, or lists of information. Think of a variable as a labeled box where you can place a value and refer back to it later using its name. In PHP, every variable name must start with a dollar sign, followed by the variable name itself.

Unlike some other programming languages, PHP Variables do not require you to declare a specific type in advance. PHP automatically detects the type based on the value you assign, which makes it beginner-friendly and flexible to work with.

Declaring PHP Variables

Declaring PHP Variables is simple. You use the dollar sign followed by a name, then an equals sign, and finally the value you want to store. Here is a basic example:

<?php
$name = "Ali";
$age = 25;
$price = 99.99;

echo $name;
echo $age;
echo $price;
?>

In this example, three different PHP Variables are created: one holding text, one holding a whole number, and one holding a decimal number. PHP automatically understands the type of each variable without any extra instructions from you.

Naming Rules for PHP Variables

Before you start creating PHP Variables freely, it is important to understand the naming rules PHP enforces. Breaking these rules will cause errors in your script.

  • A variable name must start with a dollar sign followed by a letter or underscore
  • After the first character, you can use letters, numbers, and underscores
  • Variable names cannot start with a number
  • Variable names are case-sensitive, meaning $Name and $name are treated differently
  • Spaces are not allowed inside variable names — use underscores instead

Following these naming conventions consistently will help you avoid confusing bugs as your PHP Variables grow in number across larger projects.

Understanding PHP Data Types

Every value stored in PHP Variables belongs to a specific data type. PHP supports several core data types that you will use constantly throughout this series. Let’s explore each one with practical examples.

String Data Type

A string is simply text, wrapped inside either single or double quotes. Strings are one of the most commonly used data types when working with PHP Variables.

<?php
$city = "Karachi";
$greeting = 'Hello World';
echo $city;
?>

Integer Data Type

An integer is a whole number without any decimal point. It can be positive or negative. Integers are frequently used for counting, indexing, and mathematical operations involving PHP Variables.

<?php
$students = 30;
$temperature = -5;
echo $students;
?>

Float (Double) Data Type

A float, also called a double, is a number that contains a decimal point. This data type is essential when your PHP Variables need to store prices, percentages, or measurements that require precision.

<?php
$price = 149.50;
$percentage = 87.5;
echo $price;
?>

Boolean Data Type

A boolean represents one of only two possible values: true or false. Boolean values are extremely useful when controlling logic and decision-making inside your programs, which you will explore further in upcoming tutorials.

<?php
$isLoggedIn = true;
$hasError = false;
?>

Array Data Type

An array allows a single variable to store multiple values at once. Arrays are incredibly powerful and will be covered in much greater depth in a dedicated tutorial later in this series.

<?php
$colors = array("Red", "Green", "Blue");
echo $colors[0];
?>

NULL Data Type

NULL represents a variable that has no value assigned to it. It is different from zero or an empty string — NULL specifically means the variable is empty or undefined.

<?php
$middleName = null;
?>

Checking Data Types with gettype()

PHP provides a built-in function called gettype() that lets you check the current data type of any variable. This is a handy tool while you are still learning how PHP Variables behave.

<?php
$score = 95;
echo gettype($score);
?>

Running this code will output “integer”, confirming the data type PHP has assigned to that variable automatically. For complete official documentation on all supported types, visit the PHP data types manual.

What’s Next?

You now understand how PHP Variables work, how to name them correctly, and the core data types PHP supports. In the next tutorial, we will dive into PHP Operators, where you’ll learn how to perform calculations and comparisons using the variables you just learned to create.

If you missed the previous lesson, check out Tutorial 1: PHP Installation Guide, or explore all lessons in our PHP category.

Practice Exercise

Complete the following tasks to reinforce what you learned in this tutorial:

  • Create a PHP file named variables-practice.php
  • Declare a string variable storing your full name
  • Declare an integer variable storing your age
  • Declare a float variable storing your favorite product’s price
  • Declare a boolean variable that represents whether you are a student
  • Echo all four variables, each on a separate line using a line break
  • Use gettype() on each variable and echo the result to confirm the correct data type

Bonus Challenge: Create an array variable containing the names of five countries you would like to visit, then echo only the third country in the array using its index number.

About Author

Arshad Sultan

Arshad Sultan is the Founder of Tutorials Ocean and a Technical Architect & Full-Stack Developer with 15+ years of experience in PHP, Laravel, CRM development, API integrations, and AI-powered automation. He also runs Rapid Code, a software and AI automation agency, and has trained hundreds of students across Karachi's IT institutes since 2010.

Leave a Reply

Your email address will not be published. Required fields are marked *