Remove the echo "$str";
. Once a script outputs text (like echo "$str";
) you can no longer set headers. Headers must be set before any output is sent.
For the redirection part, you probably want something like:
header("Location: https://www.google.com/$str");
Note that html in your script is also "output" so all header calls must also come before any html. With that in mind, the full file might look like the below. Its worth noting here though that the html is a bit useless given the user will always be redirected to another page.
<?php
$str = "xyz";
for ($i = 0, $c = strlen($str); $i < $c; $i++) {
$str[$i] = (rand(0, 100) > 50 ? strtoupper($str[$i]) : strtolower($str[$i]));
}
header("Location: http://www.google.com/$str");
?>
<html>
<head>
<title>PHP Test</title>
</head>
<body>
</body>
</html>