Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Ds\Stack::isEmpty() Function



The PHP Ds\Stack::isEmpty() function is used to determine whether the current stack is empty. The empty stack refers to 0 elements present in it.

This function returns a boolean value 'true', if the current instance is empty ([]), otherwise, it will return 'false'. If you invoke the count() function on an empty stack, it returns 0.

Syntax

Following is the syntax of the PHP Ds\Stack::isEmpty() function −

public Ds\Stack::isEmpty(): bool

Parameters

This function does not accept any parameter.

Return value

This function returns 'true' if the stack is empty, or 'false' otherwise.

Example 1

The following is the basic example of the PHP Ds\Stack::isEmpty() function −

<?php    
   $stack = new \Ds\Stack();
   echo "The stack elements are: \n";
   print_r($stack);
   echo "Is stack is empty? ";
   #using isEmpty() function
   var_dump($stack->isEmpty());   
?>

Output

After executing the above program, the following output will be displayed −

The stack elements are:
Ds\Stack Object
(
)
Is stack is empty? bool(true)

Example 2

If the current stack is not empty, the isEmpty() function returns 'false' −

Following is another example of the PHP Ds\Stack::isEmpty() function. We use this function to check whether this stack ([1, 2, 3]) is empty −

<?php    
   $stack = new \Ds\Stack([1, 2, 3]);
   echo "The stack elements are: \n";
   print_r($stack);
   echo "Is stack is empty? ";
   #using isEmpty() function
   var_dump($stack->isEmpty());   
?>

Output

The above program produces the following output −

The stack elements are:
Ds\Stack Object
(
    [0] => 3
    [1] => 2
    [2] => 1
)
Is stack is empty? bool(false)

Example 3

Using theisEmpty()function results in the conditional statement to check whether the current stack is empty or not −

<?php    
   $stack = new \Ds\Stack(["Tutorials", "Point", "India"]);
   echo "The stack elements are: \n";
   print_r($stack);
   #using the isEmpty() function
   $res = $stack->isEmpty();
   if($res){
	   echo "Stack is empty";
   }
   else{
	   echo "Stack is non-empty";
   }
?>

Output

Once the above program is executed, the following output will be displayed −

The stack elements are:
Ds\Stack Object
(
    [0] => India
    [1] => Point
    [2] => Tutorials
)
Stack is non-empty
php_function_reference.htm
Advertisements