September 13, 2026
PHP Variables and Data Types: A Beginner’s Guide
If you’re starting your journey into PHP development, one of the first concepts you need to understand is variables and data types.
By Yashbodar
3 min read
Variables allow you to store information, while data types define what kind of information you're working with. Whether you're building a simple website, a login system, or a Laravel application, these concepts are essential.
In this guide, we'll explore PHP variables and its most commonly used data types with simple, practical examples.
What Is a Variable in PHP?
A variable is a container used to store data. In PHP, every variable starts with a dollar sign ($), followed by its name.
Here's a simple example:
<?php
$name = "Yash";
$age = 25;
echo $name;
echo $age;<?php
$name = "Yash";
$age = 25;
echo $name;
echo $age;Output:
Yash25Yash25In this example:
$namestores a string.$agestores an integer.echodisplays the values.
Rules for Naming PHP Variables
PHP variable names follow a few simple rules:
- A variable must start with $.
- The name must begin with a letter or underscore after $.
- It cannot start with a number.
- Variable names are case-sensitive.
- Spaces are not allowed in variable names.
Valid variable names
$name = "John";
$user_age = 25;
$totalAmount = 500;$name = "John";
$user_age = 25;
$totalAmount = 500;Invalid variable names
$1name = "John"; // Invalid
$user name = "John"; // Invalid$1name = "John"; // Invalid
$user name = "John"; // InvalidA useful convention is to use descriptive names such as $userEmail instead of unclear names like $x.
What Are Data Types?
A data type tells PHP what kind of value a variable contains.
For example:
- A person's name is text.
- A person's age is a whole number.
- A product's price may contain decimals.
- A login status can be true or false.
PHP supports several important data types.
1. String
A string is a sequence of characters, such as a name, email address, or sentence.
<?php
$name = "Yash";
$message = "Welcome to PHP";
echo $name;
echo $message;<?php
$name = "Yash";
$message = "Welcome to PHP";
echo $name;
echo $message;Strings can be written using single or double quotes:
$name = 'Yash';
$greeting = "Hello, Yash!";$name = 'Yash';
$greeting = "Hello, Yash!";2. Integer
An integer is a whole number without a decimal point.
<?php
$age = 25;
$quantity = 10;
echo $age;<?php
$age = 25;
$quantity = 10;
echo $age;Examples of integers include:
10
0
-5
100010
0
-5
10003. Float
A float is a number that contains a decimal point.
<?php
$price = 499.99;
$rating = 4.5;
echo $price;<?php
$price = 499.99;
$rating = 4.5;
echo $price;Floats are commonly used for measurements, prices, percentages, and ratings.
Note:_ For applications involving money, especially financial calculations, be careful with floating-point precision. Depending on the use case, storing amounts as integers in the smallest currency unit or using decimal-based handling may be more appropriate._
4. Boolean
A Boolean represents one of two values:
truefalse
Example:
<?php
$isLoggedIn = true;
$isAdmin = false;
var_dump($isLoggedIn);<?php
$isLoggedIn = true;
$isAdmin = false;
var_dump($isLoggedIn);Output:
bool(true)bool(true)Booleans are frequently used in conditions:
if ($isLoggedIn) {
echo "Welcome back!";
}if ($isLoggedIn) {
echo "Welcome back!";
}5. Array
An array stores multiple values in a single variable.
<?php
$colors = ["Red", "Green", "Blue"];
echo $colors[0];<?php
$colors = ["Red", "Green", "Blue"];
echo $colors[0];Output:
RedRedPHP arrays can also use key-value pairs:
<?php
$user = [
"name" => "Yash",
"email" => "yash@example.com",
"age" => 25
];
echo $user["name"];<?php
$user = [
"name" => "Yash",
"email" => "yash@example.com",
"age" => 25
];
echo $user["name"];Output:
YashYashArrays are extremely useful when working with lists of products, users, database results, and API responses.
6. NULL
NULL represents a variable with no value.
<?php
$middleName = null;
var_dump($middleName);<?php
$middleName = null;
var_dump($middleName);Output:
NULLNULLA variable can also be set to NULL when its previous value is no longer needed.
How to Check a Variable's Data Type
PHP provides the var_dump() function to display a variable's type and value.
<?php
$name = "Yash";
$age = 25;
$price = 99.99;
$isActive = true;
var_dump($name);
var_dump($age);
var_dump($price);
var_dump($isActive);<?php
$name = "Yash";
$age = 25;
$price = 99.99;
$isActive = true;
var_dump($name);
var_dump($age);
var_dump($price);
var_dump($isActive);Output:
string(4) "Yash"
int(25)
float(99.99)
bool(true)string(4) "Yash"
int(25)
float(99.99)
bool(true)This is especially useful when debugging code.
A Practical Example: Product Information
Let's combine variables and data types in a small example:
<?php
$productName = "Wireless Mouse";
$price = 799.99;
$stock = 15;
$isAvailable = true;
echo "Product: " . $productName . "<br>";
echo "Price: ₹" . $price . "<br>";
echo "Stock: " . $stock . "<br>";
echo "Available: " . ($isAvailable ? "Yes" : "No");<?php
$productName = "Wireless Mouse";
$price = 799.99;
$stock = 15;
$isAvailable = true;
echo "Product: " . $productName . "<br>";
echo "Price: ₹" . $price . "<br>";
echo "Stock: " . $stock . "<br>";
echo "Available: " . ($isAvailable ? "Yes" : "No");Output:
Product: Wireless Mouse
Price: ₹799.99
Stock: 15
Available: YesProduct: Wireless Mouse
Price: ₹799.99
Stock: 15
Available: YesHere, we used:
- String for the product name
- Float for the price
- Integer for stock
- Boolean for availability
Conclusion
Variables and data types are the foundation of PHP programming. Once you understand how to store and work with different kinds of data, learning conditions, loops, functions, and Laravel becomes much easier.
In the next article, we'll explore PHP functions and learn how to write reusable code.
Have you started learning PHP recently? Share what you're building in the comments.