What are the potential pitfalls of using functions like strpos and substr_replace in PHP?

Using functions like strpos and substr_replace in PHP can be error-prone as they may return false or unexpected results if the search string is not found or if the start position is negative. To avoid potential pitfalls, it's important to check the return value of strpos before using it and handle edge cases appropriately. Similarly, when using substr_replace, make sure to validate the input parameters to prevent unexpected behavior.

// Example of using strpos and substr_replace with error handling

$string = "Hello, World!";
$search = "World";
$position = strpos($string, $search);

if ($position !== false) {
    $replacement = "Universe";
    $new_string = substr_replace($string, $replacement, $position, strlen($search));
    echo $new_string;
} else {
    echo "Search string not found.";
}