In PHP, you can use an array to create a collection of unique strings.
In this example,
stringSet
to store strings.<?php
// Create an array of strings
$stringSet = array();
// Add strings to the array
$stringSet[] = 'Apple';
$stringSet[] = 'Banana';
$stringSet[] = 'Cherry';
// Print the array
print_r($stringSet);
?>
Array ( [0] => Apple [1] => Banana [2] => Cherry )
In this example,
in_array
function to check if a specific string exists in the array.in_array
function returns true if the string is in the array, otherwise false.<?php
// Check if the array contains a string
$containsBanana = in_array('Banana', $stringSet);
echo 'Array contains Banana: ';
var_export($containsBanana);
?>
Array contains Banana: true
In this tutorial, we learned How to create a Set of Strings in PHP language with well detailed examples.