What are the best practices for handling the "needle" parameter in PHP functions like strpos?

When using functions like strpos in PHP, it is important to handle the "needle" parameter carefully to avoid unexpected behavior. One best practice is to always ensure that the needle parameter is of the correct data type and format expected by the function. Additionally, it is recommended to validate the input before passing it to the function to prevent errors or vulnerabilities in the code.

$haystack = "Hello, world!";
$needle = "world";

if (is_string($needle)) {
    $pos = strpos($haystack, $needle);
    
    if ($pos !== false) {
        echo "Needle found at position: " . $pos;
    } else {
        echo "Needle not found";
    }
} else {
    echo "Invalid needle parameter";
}