PHP Variables and Data Types

11/9/2025
All Articles

PHP variables and data types with code examples

PHP Variables and Data Types

PHP Variables and Data Types – Complete PHP Tutorial for Beginners

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.


What is a Variable in PHP?

A variable is a memory storage container that holds data. In PHP, every variable starts with a $ symbol.

Example:

<?php
$name = "John";
$age = 25;
?>

Here:

  • $name is a variable storing text

  • $age is a variable storing a number


Rules for Writing PHP Variables

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 Data Types

PHP supports multiple data types. These help define the type of value stored inside variables.

1. String

Stores text inside quotes.

<?php
$language = "PHP";
?>

2. Integer

Stores whole numbers.

<?php
$year = 2025;
?>

3. Float (Double)

Stores decimal numbers.

<?php
$price = 99.99;
?>

4. Boolean

Can store only true or false.

<?php
$isLoggedIn = true;
?>

5. Array

Stores multiple values in one variable.

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

6. Object

Stores instance of a class.

<?php
class Car {
  public $model = "Toyota";
}
$car = new Car();
?>

7. NULL

Represents an empty variable value.

<?php
$user = null;
?>

Displaying Variable Using echo

You can display stored value using echo keyword.

<?php
$name = "Shubham";
echo $name;
?>

Conclusion

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.

Article