//str_contains ( string $haystack , string $needle ) : bool
if (str_contains('Foo Bar Baz', 'Foo')) {
echo 'Found';
}
<?php
$food = 'cake';
$return_value = match ($food) {
'apple' => 'This food is an apple',
'bar' => 'This food is a bar',
'cake' => 'This food is a cake',
};
var_dump($return_value);
?>
<?php
$age = 23;
$result = match (true) {
$age >= 65 => 'senior',
$age >= 25 => 'adult',
$age >= 18 => 'young adult',
default => 'kid',
};
var_dump($result);
?>
<?php
$return_value = match (subject_expression) {
single_conditional_expression => return_expression,
conditional_expression1, conditional_expression2 => return_expression,
};
$food = 'cake';
$return_value = match ($food) {
'apple' => 'This food is an apple',
'bar' => 'This food is a bar',
'cake' => 'This food is a cake',
};
var_dump($return_value);
?>
echo match (8.0) {
'8.0' => "Oh no!",
8.0 => "This is what I expected",
};
//> This is what I expected
// TurkoSoft
echo match ($x) {
0 => 'Car',
1 => 'Bus',
2 => 'Bike',
};
You could use regular expressions as its better for word matching compared to
strpos, as mentioned by other users. A strpos check for are will also return
true for strings such as: fare, care, stare, etc. These unintended matches can
simply be avoided in regular expression by using word boundaries.
A simple match for are could look something like this:
$a = 'How are you?';
if (preg_match('/are/', $a)) {
echo 'true';
}