Introduction
In PHP, variables can store different types of data such as numbers, text, arrays, and objects. Understanding data types is essential for writing efficient and error-free code.
PHP is a loosely typed (weakly typed) language, which means you do not need to declare the data type of a variable explicitly. PHP automatically determines the data type based on the value assigned to the variable.
What is a PHP Data Type?
A PHP Data Type defines the type of data that a variable can store. It determines how the data is processed and what operations can be performed on it.
For example:
- Numbers → Used for calculations
- Strings → Used for text
- Arrays → Used for multiple values
- Objects → Used in object-oriented programming
Note: PHP automatically assigns a data type to a variable based on its value.
Types of PHP Data Types
PHP supports the following 8 data types:
- Integer
- Float (Double)
- String
- Array
- Object
- Boolean
- NULL
- Resource
Syntax
You can use the var_dump() function to check the data type and value of a variable:
<?php
$var = 10;
var_dump($var);
?>
1. Integer
An Integer is a whole number without a decimal point.
- Range: -2,147,483,648 to 2,147,483,647
- Can be positive or negative
Example:
<?php
$num = 2500;
var_dump($num);
?>
Output:
2. Float (Double)
A Float (or double) is a number with a decimal point.
Example:
<?php
$num = 2.5;
var_dump($num);
?>
Output:
3. String
A String is a sequence of characters such as text, numbers, or symbols.
Single Quotes Example
<?php
$employee_name = 'John Taylor';
echo $employee_name;
?>
Output:
Double Quotes Example (Supports Variables)
Example 1:
<?php
$employee_name = "John Taylor";
echo $employee_name;
?>
Output:
Example 2:
<?php
$employee_name = 'John Taylor';
$employee_greetings = "Hello $employee_name";
echo $employee_greetings;
?>
Output:
4. Array
An Array is a collection of multiple values stored in a single variable.
Indexed Array Example
<?php
$employee_name = array('John', 'Rom', 'Mathew');
echo $employee_name[0];
echo $employee_name[1];
echo $employee_name[2];
?>
Output:
Associative Array Example
<?php
$employee = array(
'name' => 'John',
'age' => 25,
'designation' => 'Software Engineer'
);
echo $employee['name'];
echo $employee['age'];
echo $employee['designation'];
?>
5. Object
An Object is an instance of a class. It contains properties and methods.
Example:
<?php
class Employee {
public $empname;
function display_empname($empname) {
return $empname;
}
}
$emp_obj = new Employee;
echo $emp_obj->display_empname('John');
?>
Output:
6. Boolean
A Boolean represents two possible values:
- true (1)
- false (0)
Example:
<?php
$a = true;
$b = false;
?>
Boolean values are mostly used in:
- Conditional statements (if, else)
- Loops
7. NULL
The NULL data type represents a variable with no value.
Example:
<?php
$x = NULL;
if(isset($x)){
echo 'success';
} else {
echo 'fail';
}
?>
Output:
Note: isset() returns false if the variable is NULL.
8. Resource
A Resource is a special data type that holds references to external resources such as:
- Database connections
- File handles
- Network connections
Example:
<?php
$file = fopen("test.txt", "r");
var_dump($file);
?>
You cannot directly manipulate resources. They are handled using built-in functions.
Real-Life Example
Example: User Profile System
<?php
$name = "John"; // String
$age = 35; // Integer
$isLoggedIn = true; // Boolean
$user = array(
"name" => $name,
"age" => $age
);
echo "Welcome " . $user['name'];
?>
This example uses multiple data types together in a real-world scenario.
Common Mistakes
1. Confusing Integer and String
$age = "25"; // String, not integer
2. Forgetting Quotes in Strings
$name = John; // ❌ Error
3. Accessing Array Without Index
echo $arr; // ❌ Error
4. Not Checking NULL Values
Using variables without checking NULL can cause issues.
5. Misusing Boolean Values
Confusing true/false with strings like “true”.
Conclusion
PHP data types are essential for handling different kinds of data in web applications.
PHP automatically manages data types, making it easier for beginners to learn. However, understanding how each data type works is important to avoid errors and write efficient code.
PHP Data Type – Interview Questions
Q 1: What are data types in PHP?
Q 2: Name basic PHP data types.
Q 3: Is PHP strongly typed?
Q 4: How to check variable data type?
Q 5: Can PHP variables change data type?
PHP Data Type – Objective Questions (MCQs)
Q1. Which of the following is a scalar data type in PHP?
Q2. Which data type represents true/false values?
Q3. PHP supports how many main data types?
Q4. Which of the following is a compound type?
Q5. Which type is returned by gettype(5.5)?