The goal is to display “Hello” followed by the name of the person. The name has to be specified in the URL then the PHP has to take the information from the URL and greet the user. If the user does not specify his name the code displays “hello platypus”.
It seems like the method I’m using is not working, and since I took it from php.net there’s no other example to help me. here’s the code:
<?php
$name = htmlspecialchars($_GET["name"]);
if (isset($name)) {
echo '<p>Hello ' . $name . "</p>";
} else {
echo '<p>Hello platipus</p>';
}
?>
the URL that I should use to get things working is: http://localhost/index.php?name=Martin
but when I just use localhost:8000 it gives me an error:
Warning: Undefined array key "name" in /Users/filou/Desktop/mySSHadress/ex_02/index.php on line 4
Deprecated: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in /Users/filou/Desktop/mySSHadress/ex_02/index.php on line 4
Hello
So as you can see it prints the hello, but not the name. I think the first part of the error (the warning) is due to the fact that there’s no name that the $_GET can take from the URL. The second error (the deprecated) is — if I understand well — due to the fact that I try to echo an array, while I should try to echo a string. That’s why I tried to echo $name[0] instead of $name alone. But it doesn’t work either. Do you have any ideas? Thanks for the time you took to read all that. Have a nice day.