How can the FindPosition function be optimized to avoid potential pitfalls in PHP scripts?

The FindPosition function can be optimized by using strict type declarations in the function parameters to ensure that only the correct data types are accepted. This helps avoid potential pitfalls such as unexpected type conversions or errors due to mismatched data types. By explicitly specifying the data types, the function becomes more predictable and less error-prone.

function FindPosition(int $needle, array $haystack): int {
    $position = array_search($needle, $haystack);
    return $position !== false ? $position : -1;
}