PHP Data Types
In PHP, data types define the kind of value a variable can hold. PHP is a loosely typed language, meaning you don’t need to declare the data type of a variable. It is automatically assigned based on the value. But it is important to understand data types because it is important for writing reliable, bug-free, and efficient code.
PHP supports 8 main data types, grouped into three categories:
- Scaler Data Types
- Compound Data Types
- Special Data Types

PHP Data Types
Scaler Data Types
Scalar types hold single values. These are the basic and commonly used data types in PHP. They store only one value at a time, such as a number or a string of text.
1. Integer
An integer is a whole number (positive or negative) without decimals.
Now, let us understand with the help of the example:
<?php
$age = 25;
echo $age; // Output: 25
?>
Output
25
2. Float (Double)
A float is a number with a decimal point or in exponential form.
Now, let us understand with the help of the example:
<?php
$price = 99.99;
echo $price; // Output: 99.99
?>
Output
99.99
3. String
A string is a sequence of characters, enclosed in single or double quotes.
Now, let us understand with the help of the example:
<?php
$name = "GeeksforGeeks";
echo $name; // Output: GeeksforGeeks
?>
Output
GeeksforGeeks
Note: Double-quoted strings allow variable parsing; single-quoted strings don’t.
<?php
$greeting = "Hello";
echo "Message: $greeting \n"; // Message: Hello
echo 'Message: $greeting'; // Message: $greeting
?>
Output
Message: Hello Message: $greeting
To learn more about PHP Strings read this article – PHP Strings
4. Boolean
A boolean represents either true or false. It is useful for conditional statements and logic control.
Now, let us understand with the help of the example:
<?php
$isActive = true;
if ($isActive) {
echo "Active";
}
?>
Output
Active
Compound Data Types
1. Array
Compound types hold multiple values or are used to structure data more meaningfully. These data types allow you to group several values together.
Now, let us understand with the help of the example:
<?php
$colors = ["red", "green", "blue"];
echo $colors[1]; // Output: green
?>
Output
green
To learn more about PHP Array read this article – PHP Arrays
2. Objects
An object is an instance of a class and is used to access methods and properties. Objects are explicitly declared and created from the new keyword.
Now, let us understand with the help of the example:
<?php
class Car {
public $brand = "Toyota";
}
$car = new Car();
echo $car->brand; // Output: Toyota
?>
Output
Toyota
To learn more about PHP Obejcts read this article – PHP Objects
Special Data Types
Special types handle unique cases such as representing empty values or connecting with external resources.
1. NULL
A NULL value means the variable has no value. It is unset using unset().
Now, let us understand with the help of the example:
<?php
$x = NULL;
var_dump($x); // Output: NULL
?>
Output
NULL
2. Resources
A resource is a special variable that holds a reference to an external resource, such as a file or database connection.
<?php
$handle = fopen("file.txt", "r");
var_dump($handle);
?>
Output
Warning: fopen(file.txt): failed to open stream: No such file or directory in /home/guest/sandbox/Solution.php on line 2 bool(false)
Note: To check the type and value of an expression, use the var_dump() function which dumps information about a variable.
PHP Functions to Check Data Types
PHP provides several functions to check the type of variables, such as:
- is_int(): Checks if a variable is an integer.
- is_string(): Checks if a variable is a string.
- is_array(): Checks if a variable is an array.
- is_object(): Checks if a variable is an object.
- is_bool(): Checks if a variable is a boolean.
- is_null(): Checks if a variable is NULL.
Best Practices for using the Data Types
- Know the type of data your variable holds.
- Use var_dump() or gettype() for debugging.
- Avoid mixing types unless necessary.
- Validate and sanitize data when working with user inputs.
Conclusion
PHP data types are the foundation of how information is stored and handled in your scripts. From simple values like numbers and strings to complex structures like arrays and objects, understanding these types helps you write better and more efficient code. Since PHP automatically assigns data types, it’s important to know how they work, how to check them, and how to manage conversions when needed. Mastering data types ensures your programs run smoothly and behave as expected.