To check if a specific item is present in a Set in PHP, you can use the in_array
function.
In this example,
mySet
containing elements of a specific type.in_array
function to check if a specific item is present in the array.in_array
function returns true
if the item is present in the array, otherwise it returns false
.<?php
// Define array with items
$mySet = ["apple", "banana", "orange", "grape", "melon"];
// Check if item is present
$item = "banana";
$isPresent = in_array($item, $mySet);
// Print result
echo "Is $item present in the Set? ", $isPresent ? 'true' : 'false';
?>
Is banana present in the Set? true
In this example,
mySet
containing elements of a specific type.in_array
function to check if a specific item is not present in the array.in_array
function returns false
if the item is not present in the array.<?php
// Define array with items
$mySet = ["apple", "banana", "orange", "grape", "melon"];
// Check if item is not present
$item = "peach";
$isNotPresent = !in_array($item, $mySet);
// Print result
echo "Is $item not present in the Set? ", $isNotPresent ? 'true' : 'false';
?>
Is peach not present in the Set? true
In this tutorial, we learned How to check if a Specific Item is present in the Set in PHP language with well detailed examples.