How do you use the indexOf function in PHP?

In PHP, there is no built-in indexOf function. However, you can use the strpos() function to find the first occurrence of a string within another string.

The usage method of the strpos() function is as follows:

$string = "Hello, world!";
$substring = "world";

$position = strpos($string, $substring);

if ($position !== false) {
    echo "The substring was found at position: " . $position;
} else {
    echo "The substring was not found";
}

The code above will display: The substring was found at position 7. This indicates that the substring “world” was found in the string “Hello, world!” at the position of the 7th character. If the substring is not found in the string, the strpos() function will return false.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds