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

PHP - Ds Deque::contains() Function



The PHPDs\Deque::contains()function is used to determine whether the deque contains given values. This function returns a boolean 'true', if the current deque contains the specified value; otherwise, it will return 'false'.

Syntax

Following is the syntax of the PHP Ds\Deque::contains() function −

public Ds\Deque::contains(mixed ...$values): bool

Parameters

This function accepts a single parameter named 'values', which is described below −

  • values − The values needs to check.

Return value

This function returns 'true' if any of the provided values are present in the deque, 'false' otherwise.

Example 1

The following is the basic example of the PHP Ds\Deque::contains() function −

<?php 
   $deque = new \Ds\Deque([1, 2, 3, 4, 5]);
   echo("The deque elements are: \n"); 
   print_r($deque);
   $val = 2;
   echo "The given value: ".$val;
   echo "\nIs deque contain the element ".$val." or not? "; 
   var_dump($deque->contains($val)); 
?>

Output

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

The deque elements are:
Ds\Deque Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The given value: 2
Is deque contain the element 2 or not? bool(true)

Example 2

If the specified element is not present in a curent deque, the Ds\Deque::contains() function returns 'false' −

<?php 
   $deque = new \Ds\Deque(["Tutorials", "Point", "India"]);
   echo("The deque elements are: \n"); 
   print_r($deque);
   $val = "Tutorix";
   echo "The given value: ".$val;
   echo "\nIs deque contain the element '".$val."' or not? "; 
   var_dump($deque->contains($val)); 
?>

Output

The above program displays the following output −

The deque elements are:
Ds\Deque Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
The given value: Tutorix
Is deque contain the element 'Tutorix' or not? bool(false)

Example 3

Using the function "result" within the conditional statement to check whether the specified value 'e' is found in a deque or not −

<?php 
   $deque = new \Ds\Deque(['a', 'e', 'i', 'o', 'u']);
   echo("The deque elements are: \n"); 
   print_r($deque);
   $val = 'e';
   echo "The given value: ".$val;
   echo "\nIs the element '".$val."' found in deque or not? "; 
   $result = $deque->contains($val);
   if($result){
	   echo "Found";
   }	   
   else{
	   echo "Not found";
   }
?>

Output

Once the above program is executed, it generates the following output −

The deque elements are:
Ds\Deque Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
The given value: e
Is the element 'e' found in deque or not? Found
Advertisements