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

PHP - Ds Sequence::contains() Function



The PHP Ds\Sequence::contains() function is used to determine whether the specified values are present in a sequence or sequence containing those values.

This function returns a boolean value 'true' if the value is present in a sequence otherwise, it returns 'false'. It allows you to check single or multiple values at a time.

Syntax

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

public abstract bool Ds\Sequence::contains([ mixed $...values ] )

Parameters

Following is the parameter of this function −

  • values − Single or multiple values need to be checked.

Return value

This function returns false if any of the provided values are not in a sequence, or true otherwise.

Example 1

Following is the basic example of the PHP Ds\Sequence::contains() function −

<?php
   $seq = new \Ds\Vector([1, 2, 3, 4, 5]);
   echo "The sequence elements are: \n";
   print_r($seq);
   $val = 2;
   echo "The value needs to be checked: ".$val;
   
   #using contains() function
   echo "\nIs value ".$val." present in a sequence: ";
   var_dump($seq->contains($val));
?>

Output

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

The sequence elements are:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The value needs to be checked: 2
Is value 2 present in a sequence: bool(true)

Example 2

If the sequence does not contain the specified value, this function returns 'false'.

Following is another example of the PHP Ds\Sequence::contains() function. We use this function to determine whether this sequence (["Tutorials", "Point", "India"]) contains the value "Tutorix" −

<?php
   $seq = new \Ds\Vector(["Tutorials", "Point", "India"]);
   echo "The sequence elements are: \n";
   print_r($seq);
   $val = "Tutorix";
   echo "The value needs to be checked: ".$val;
   #using contains() function
   echo "\nIs value ".$val." present in a sequence: ";
   var_dump($seq->contains($val));
?>

Output

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

The sequence elements are:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
The value needs to be checked: Tutorix
Is value Tutorix present in a sequence: bool(false)

Example 3

Checking multiple values at a time.

In the example below, we use the contains() function to determine if the specified values are present in a sequence −

<?php
   $seq = new \Ds\Vector(['a', 'b', 'c', 'd', 'e']);
   echo "The sequence elements are: \n";
   print_r($seq);
   echo "Is values 'a', 'b' and 'c' present in a sequence: ";
   var_dump($seq->contains('a', 'b', 'c'));
?>

Output

The above program produces the following output −

The sequence elements are:
Ds\Vector Object
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
)
Is values 'a', 'b' and 'c' present in a sequence: bool(true)
php_function_reference.htm
Advertisements