Lesson 7 of 50 – Data Types in PHP
14%

Data Types in PHP

Data types define the kind of value stored in a variable. PHP supports several data types, including strings, integers, floats, Booleans, arrays, objects, NULL, and resources.

Note: PHP is dynamically typed. You can assign different types of values to variables without declaring their types in advance.
What are Data Types in PHP?

A data type tells PHP what kind of value a variable contains. Different data types are suitable for storing different kinds of information.

Example:
<?php
$name = "Rahul";
$age = 20;
$price = 99.50;
$is_student = true;

var_dump($name);
var_dump($age);
var_dump($price);
var_dump($is_student);
?>
Output:
string(5) "Rahul"
int(20)
float(99.5)
bool(true)

The var_dump() function displays the type and value of each variable.

String Data Type

A string is a sequence of characters. Strings are commonly used to store names, addresses, messages, and other text.

Example:
<?php
$name = "Rahul";
$city = 'Patna';

echo $name;
echo "<br>";
echo $city;
?>
Output:
Rahul
Patna

PHP strings can be enclosed in single or double quotation marks. Double-quoted strings can interpolate variables, while single-quoted strings generally treat variable names as literal text.

Integer Data Type

An integer is a whole number without a decimal part. Integers can be positive, negative, or zero.

Example:
<?php
$age = 20;
$temperature = -5;
$count = 0;

echo $age;
echo "<br>";
echo $temperature;
echo "<br>";
echo $count;
?>
Output:
20
-5
0

Integers are useful for storing ages, quantities, counts, and other whole-number values.

Float Data Type

A float, also called a floating-point number, represents a number that can contain a fractional part.

Example:
<?php
$price = 99.50;
$percentage = 85.75;
$temperature = -2.5;

echo $price;
echo "<br>";
echo $percentage;
echo "<br>";
echo $temperature;
?>
Output:
99.5
85.75
-2.5

PHP may display a floating-point value without unnecessary trailing zeros. For example, 99.50 may be displayed as 99.5.

Boolean Data Type

A Boolean value represents one of two states: true or false. Booleans are often used in conditions and decision-making.

Example:
<?php
$is_student = true;
$is_admin = false;

var_dump($is_student);
var_dump($is_admin);
?>
Output:
bool(true)
bool(false)

The var_dump() function displays Boolean values as bool(true) and bool(false).

Array Data Type

An array stores multiple values in a single variable. PHP arrays can contain values of different types.

Example:
<?php
$colors = ["Red", "Green", "Blue"];

echo $colors[0];
echo "<br>";
echo $colors[1];
echo "<br>";
echo $colors[2];
?>
Output:
Red
Green
Blue

PHP array indexes start at zero for this indexed array. Therefore, $colors[0] contains the first item.

Object Data Type

An object is an instance of a class. Objects can contain properties and methods that represent data and behavior.

Example:
<?php
class Student {
    public $name = "Rahul";
}

$student = new Student();

echo $student->name;
?>
Output:
Rahul

The new keyword creates an object from the Student class. The arrow operator (->) accesses the object's property.

NULL and Resource Data Types
NULL Data Type

The NULL value represents a variable with no value.

<?php
$result = null;

var_dump($result);
?>
Output:
NULL
Resource Data Type

A resource is a special value that refers to an external resource managed by PHP, such as an open file handle. Resources are commonly encountered when working with files or certain system operations.

Example:
<?php
$file = fopen("example.txt", "w");

if ($file !== false) {
    var_dump($file);
    fclose($file);
}
?>

This example opens a file for writing, checks whether opening succeeded, displays the resource information, and closes the file.

Important: The example creates or overwrites example.txt in the current working directory. Use a suitable test directory when practising file operations.
Summary of PHP Data Types
Data Type Example Purpose
String "Rahul" Stores text
Integer 20 Stores whole numbers
Float 99.50 Stores floating-point numbers
Boolean true Stores true or false
Array [1, 2, 3] Stores multiple values
Object new Student() Stores an object instance
NULL null Represents no value
Resource fopen(...) References an external resource

You can use gettype() to obtain a variable's type name or var_dump() to inspect its type and value.

Example:
<?php
$value = 100;

echo gettype($value);
?>
Output:
integer
Practical Assignment

Complete the following tasks:

  1. Create a string variable to store your name.
  2. Create an integer variable to store your age.
  3. Create a float variable to store a price.
  4. Create a Boolean variable to represent a true or false condition.
  5. Create an array containing three subject names.
  6. Create a variable with the value null.
  7. Use var_dump() to inspect your variables.
Practice Example:
<?php
$name = "Rahul";
$age = 20;
$price = 150.75;
$is_student = true;
$subjects = ["PHP", "HTML", "CSS"];
$result = null;

var_dump($name);
var_dump($age);
var_dump($price);
var_dump($is_student);
var_dump($subjects);
var_dump($result);
?>

🧠 Quick Quiz

1. Which PHP data type stores text?




2. Which function displays a variable's type and value?




3. Which value represents a Boolean false value?