PHP Variables and Data Types
PHP variables and data types with code examples
In PHP programming, variables and data types are among the most important fundamentals. Before building dynamic pages or processing form data, you must understand how variables work and which data types you can store in PHP. This tutorial explains PHP variables, their rules, and all the major data types used in PHP.
A variable is a memory storage container that holds data. In PHP, every variable starts with a $ symbol.
<?php
$name = "John";
$age = 25;
?>
Here:
$name is a variable storing text
$age is a variable storing a number
| Rule | Explanation |
|---|---|
Must start with $ |
Every variable should begin with $ symbol |
| Cannot start with a number |
$1name is invalid |
| Must start with letter or underscore |
$name or $_data is allowed |
| Case-sensitive |
$Data and $data are different |
PHP supports multiple data types. These help define the type of value stored inside variables.
Stores text inside quotes.
<?php
$language = "PHP";
?>
Stores whole numbers.
<?php
$year = 2025;
?>
Stores decimal numbers.
<?php
$price = 99.99;
?>
Can store only true or false.
<?php
$isLoggedIn = true;
?>
Stores multiple values in one variable.
<?php
$colors = array("Red", "Green", "Blue");
?>
Stores instance of a class.
<?php
class Car {
public $model = "Toyota";
}
$car = new Car();
?>
Represents an empty variable value.
<?php
$user = null;
?>
You can display stored value using echo keyword.
<?php
$name = "Shubham";
echo $name;
?>
PHP variables and data types are the base of any PHP application. If you understand how to store text, numbers, array data, Boolean values, and objects properly, you can easily move to the next chapters such as operators, conditions, loops, and functions.