Introduction
PHP data types define the kind of value a variable can store, such as text, numbers, true or false, or a collection of values. Understanding data types helps you write correct PHP programs and work with user input, calculations, and databases. In this chapter, you will practice PHP’s main data types using examples, including strings, integers, floats, booleans, arrays, objects, and NULL. PHP Data Types Practice Questions with Solutions to help you understand the concepts.
Question 1: Display the Data Type of a String
Problem Statement: Create a variable named $studentName, store a student’s name, and display its data type using gettype().
PHP Solution:
<?php
$studentName = "Rahul";
echo "Student Name: " . $studentName . "<br>";
echo "Data Type: " . gettype($studentName);
?>
Sample Output:
Student Name: Rahul
Data Type: string
Explanation: The value "Rahul" is text enclosed in quotation marks, so PHP identifies it as a string. The gettype() function returns the data type of a variable.
Concepts Covered: Strings, variables, gettype().
Question 2: Work with Integer and Float Data Types
Problem Statement: Store a student’s age and percentage in separate variables. Display their values and data types.
PHP Solution:
<?php
$age = 18;
$percentage = 87.5;
echo "Age: " . $age . "<br>";
echo "Age Type: " . gettype($age) . "<br>";
echo "Percentage: " . $percentage . "<br>";
echo "Percentage Type: " . gettype($percentage);
?>
Sample Output:
Age: 18
Age Type: integer
Percentage: 87.5
Percentage Type: double
Explanation: The value 18 is an integer because it has no decimal part. The value 87.5 is a floating-point number. PHP’s gettype() reports this type as double.
Concepts Covered: Integers, floats, gettype().
Question 3: Use the Boolean Data Type
Problem Statement: Create a variable named $isPassed and store true in it. Display its value and data type using var_dump().
PHP Solution:
<?php
$isPassed = true;
echo "Result: ";
var_dump($isPassed);
?>
Sample Output:
Result: bool(true)
Explanation: A Boolean variable stores one of two values: true or false. It is useful for checking conditions, such as whether a student passed an examination.
Concepts Covered: Boolean values, var_dump().
Question 4: Check Whether a Student Passed
Problem Statement: Store a student’s marks in a variable. Create a Boolean variable that checks whether the marks are at least 40, and display the result.
PHP Solution:
<?php
$marks = 65;
$isPassed = $marks >= 40;
echo "Marks: " . $marks . "<br>";
echo "Passed: ";
var_dump($isPassed);
?>
Sample Output:
Marks: 65
Passed: bool(true)
Explanation: The expression $marks >= 40 checks whether the marks are greater than or equal to 40. Because the marks are 65, the expression returns true, which is stored in $isPassed.
Concepts Covered: Integers, Boolean data, comparison operators.
Question 5: Store Multiple Values in an Array
Problem Statement: Create an array containing three programming languages. Display the first language and the array’s data type.
PHP Solution:
<?php
$languages = ["PHP", "Python", "Java"];
echo "First Language: " . $languages[0] . "<br>";
echo "Data Type: " . gettype($languages);
?>
Sample Output:
First Language: PHP
Data Type: array
Explanation: An array stores multiple values in a single variable. PHP uses zero-based indexing, so $languages[0] accesses the first element.
Concepts Covered: Arrays, array indexing, gettype().
Question 6: Create an Associative Array
Problem Statement: Create an associative array containing an employee’s name, department, and salary. Display each value.
PHP Solution:
<?php
$employee = [
"name" => "Amit",
"department" => "IT",
"salary" => 35000
];
echo "Name: " . $employee["name"] . "<br>";
echo "Department: " . $employee["department"] . "<br>";
echo "Salary: " . $employee["salary"] . "<br>";
echo "Data Type: " . gettype($employee);
?>
Sample Output:
Name: Amit
Department: IT
Salary: 35000
Data Type: array
Explanation: An associative array stores values using named keys instead of only numeric indexes. For example, $employee["salary"] retrieves the employee’s salary.
Concepts Covered: Associative arrays, keys, values.
Question 7: Work with the NULL Data Type
Problem Statement: Create a variable named $middleName and assign it NULL. Display its value and data type.
PHP Solution:
<?php
$middleName = NULL;
var_dump($middleName);
echo "<br>";
echo gettype($middleName);
?>
Sample Output:
NULL
NULL
Explanation: NULL represents a variable with no value assigned to it. In PHP, gettype() returns the string "NULL" for a null value. This differs from an empty string ("") and the number 0.
Concepts Covered: NULL, var_dump(), gettype().
Question 8: Compare an Integer with a Numeric String
Problem Statement: Store 100 as an integer and "100" as a string. Display their data types and compare them using var_dump().
PHP Solution:
<?php
$number = 100;
$textNumber = "100";
echo "Number Type: " . gettype($number) . "<br>";
echo "Text Number Type: " . gettype($textNumber) . "<br>";
echo "Loose Comparison: ";
var_dump($number == $textNumber);
echo "Strict Comparison: ";
var_dump($number === $textNumber);
?>
Sample Output:
Number Type: integer
Text Number Type: string
Loose Comparison: bool(true)
Strict Comparison: bool(false)
Explanation: The integer 100 and the string "100" contain equivalent numeric values. The == operator compares them loosely, while === checks both value and data type. Therefore, strict comparison returns false.
Concepts Covered: Type differences, loose comparison, strict comparison.
Question 9: Use an Object as a Data Type
Problem Statement: Create a simple Student class, make an object, assign a student’s name, and display its data type.
PHP Solution:
<?php
class Student
{
public $name;
}
$student = new Student();
$student->name = "Priya";
echo "Student Name: " . $student->name . "<br>";
echo "Data Type: " . gettype($student);
?>
Sample Output:
Student Name: Priya
Data Type: object
Explanation: A class defines the structure of an object. The new Student() statement creates an object, and -> accesses its property. PHP identifies this value as an object.
Concepts Covered: Classes, objects, properties, data types.
Question 10: Identify Multiple PHP Data Types Using var_dump()
Problem Statement: Create variables containing a string, integer, float, Boolean, array, and NULL. Use var_dump() to inspect their data types and values.
PHP Solution:
<?php
$name = "Neha";
$age = 21;
$salary = 28500.75;
$isActive = true;
$skills = ["PHP", "MySQL"];
$bonus = NULL;
var_dump($name);
var_dump($age);
var_dump($salary);
var_dump($isActive);
var_dump($skills);
var_dump($bonus);
?>
Sample Output:
string(4) "Neha"
int(21)
float(28500.75)
bool(true)
array(2) {
[0]=>
string(3) "PHP"
[1]=>
string(5) "MySQL"
}
NULL
Explanation: The var_dump() function displays information about a variable, including its type and value. For strings, it also shows the length. For arrays, it displays the elements and their indexes. This makes var_dump() especially useful when debugging PHP programs.
Concepts Covered: Multiple data types, debugging, var_dump().
Key Takeaways
- A string stores text, such as
"Hello". - An integer stores whole numbers, such as
25. - A float stores decimal numbers, such as
25.75. - A Boolean stores either
trueorfalse. - An array stores multiple values in one variable.
- An object is an instance of a class.
- NULL represents the absence of a value.
gettype()returns a variable’s type as a string.var_dump()displays a variable’s type and value.- Use
===when both value and data type must match.
Frequently Asked Questions (FAQs)
1. What are data types in PHP?
Data types identify the kind of value stored in a variable, such as text, numbers, Boolean values, arrays, objects, or NULL.
2. How many data types does PHP support?
PHP supports several data types, commonly grouped into scalar types (bool, int, float, string), compound types (including array and object), and special types such as NULL and resource. PHP also supports types such as callable and iterable.
3. What is the difference between an integer and a float?
An integer represents a whole number, such as 20. A float represents a number with a fractional or decimal part, such as 20.5.
4. What is the difference between gettype() and var_dump()?
gettype() returns the data type as a string, while var_dump() displays the type and value of a variable.
5. What is a Boolean in PHP?
A Boolean is a data type that represents either true or false. It is commonly used in conditions and comparisons.
6. What is the difference between NULL and an empty string?
NULL represents the absence of a value, whereas an empty string ("") is a string containing zero characters.
7. Can a PHP variable change its data type?
Yes. PHP is dynamically typed, so a variable can hold values of different types at different points in a program.
8. What is an array in PHP?
An array is a data structure that stores multiple values in one variable. PHP supports indexed, associative, and multidimensional arrays.
9. What does === mean in PHP?
The === operator checks whether two values are equal and have the same data type.
10. Why are PHP data types important?
Understanding data types helps prevent unexpected comparisons, calculation errors, and problems when handling form input or database values.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
