Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
vector

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

Talha Ashar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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.

RELATED TAGS

php
vector

CONTRIBUTOR

Talha Ashar
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring