1$myString = 'Hello Bob how are you?';
2if (strpos($myString, 'Bob') !== false) {
3 echo "My string contains Bob";
4}
1$string = 'The lazy fox jumped over the fence';
2
3if (str_contains($string, 'lazy')) {
4 echo "The string 'lazy' was found in the string\n";
5}
6
7
1$a = 'How are you?';
2
3if (strpos($a, 'are') !== false) {
4 echo 'true';
5}
6
1<?php
2$string = 'The lazy fox jumped over the fence';
3
4if (str_contains($string, '')) {
5 echo "Checking the existence of an empty string will always return true";
6}
7
8if (str_contains($string, 'lazy')) {
9 echo "The string 'lazy' was found in the string\n";
10}
11
12if (str_contains($string, 'Lazy')) {
13 echo 'The string "Lazy" was found in the string';
14} else {
15 echo '"Lazy" was not found because the case does not match';
16}
17
18# Checking the existence of the empty string will always return true
19# The string 'lazy' was found in the string
20# "Lazy" was not found because the case does not match
1
2<?php
3$mystring = 'abc';
4$findme = 'a';
5$pos = strpos($mystring, $findme);
6
7// Note our use of ===. Simply == would not work as expected
8// because the position of 'a' was the 0th (first) character.
9if ($pos === false) {
10 echo "The string '$findme' was not found in the string '$mystring'";
11} else {
12 echo "The string '$findme' was found in the string '$mystring'";
13 echo " and exists at position $pos";
14}
15?>
16
17