What is the DS\Vector isEmpty() method in PHP?

The isEmpty() method of the DS\Vector class in PHP checks if a vector is empty.

The process is illustrated below:

The prototype of the isEmpty() method is shown below:

bool public Ds\Vector::isEmpty( void )

Parameters

The isEmpty() method does not accept any parameters.

Return value

The isEmpty() method returns true if the vector contains no elements; otherwise, it returns false.

Example

The code below shows how the isEmpty() method works in PHP:

<?php
// initialize vectors
$firstVector = new \Ds\Vector([5, 10, 15]);
$secondVector = new \Ds\Vector();
// check if vectors are empty
print("Printing first vector:\n");
print_r($firstVector);
if($firstVector->isEmpty())
{
print("The vector is empty.\n\n");
}
else
{
print("The vector is not empty.\n\n");
}
print("Printing second vector:\n");
print_r($secondVector);
if($secondVector->isEmpty())
{
print("The vector is empty.");
}
else
{
print("The vector is not empty.");
}
?>

The code above produces the following output:

Printing first vector:
Ds\Vector Object
(
[0] => 5
[1] => 10
[2] => 15
)
The vector is not empty.

Printing second vector:
Ds\Vector Object
(
)
The vector is empty.

Note: To run this program, you need to install Data Structures for PHP on your local machine.

Explanation

First, two vectors are initialized: firstVector and secondVector.

The isEmpty() method in line 11 checks if firstVector contains any elements. Since firstVector was initialized with three integer values, the isEmpty() method returns false.

Similarly, the isEmpty() method in line 23 checks if secondVector contains any elements. Since secondVector was initialized as an empty vector, the isEmpty() method returns true.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved