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.
A data type tells PHP what kind of value a variable contains. Different data types are suitable for storing different kinds of information.
<?php
$name = "Rahul";
$age = 20;
$price = 99.50;
$is_student = true;
var_dump($name);
var_dump($age);
var_dump($price);
var_dump($is_student);
?>
string(5) "Rahul"
int(20)
float(99.5)
bool(true)
The var_dump() function displays the type and value of each variable.
A string is a sequence of characters. Strings are commonly used to store names, addresses, messages, and other text.
<?php
$name = "Rahul";
$city = 'Patna';
echo $name;
echo "<br>";
echo $city;
?>
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.
An integer is a whole number without a decimal part. Integers can be positive, negative, or zero.
<?php
$age = 20;
$temperature = -5;
$count = 0;
echo $age;
echo "<br>";
echo $temperature;
echo "<br>";
echo $count;
?>
Integers are useful for storing ages, quantities, counts, and other whole-number values.
A float, also called a floating-point number, represents a number that can contain a fractional part.
<?php
$price = 99.50;
$percentage = 85.75;
$temperature = -2.5;
echo $price;
echo "<br>";
echo $percentage;
echo "<br>";
echo $temperature;
?>
PHP may display a floating-point value without unnecessary trailing zeros. For example, 99.50 may be displayed as 99.5.
A Boolean value represents one of two states: true or false. Booleans are often used in conditions and decision-making.
<?php
$is_student = true;
$is_admin = false;
var_dump($is_student);
var_dump($is_admin);
?>
bool(true)
bool(false)
The var_dump() function displays Boolean values as bool(true) and bool(false).
An array stores multiple values in a single variable. PHP arrays can contain values of different types.
<?php
$colors = ["Red", "Green", "Blue"];
echo $colors[0];
echo "<br>";
echo $colors[1];
echo "<br>";
echo $colors[2];
?>
PHP array indexes start at zero for this indexed array. Therefore, $colors[0] contains the first item.
An object is an instance of a class. Objects can contain properties and methods that represent data and behavior.
<?php
class Student {
public $name = "Rahul";
}
$student = new Student();
echo $student->name;
?>
The new keyword creates an object from the Student class. The arrow operator (->) accesses the object's property.
The NULL value represents a variable with no value.
<?php
$result = null;
var_dump($result);
?>
NULL
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.
<?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.
example.txt in the current working directory. Use a suitable test directory when practising file operations.
| 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.
<?php
$value = 100;
echo gettype($value);
?>
Complete the following tasks:
null.var_dump() to inspect your variables.<?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);
?>
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?