What is the function of str_replace in PHP and how can it be used to replace characters in a string?

The str_replace function in PHP is used to replace occurrences of a substring within a string with another substring. It takes three parameters: the substring to search for, the substring to replace it with, and the string to search within. This function can be useful for tasks such as replacing certain characters or words within a string.

// Example of using str_replace to replace characters in a string
$string = "Hello, world!";
$new_string = str_replace("o", "0", $string);
echo $new_string; // Output: Hell0, w0rld!