Undefined index error

<a href=“welcome2.php?firstname=Kevin&lastname=Yank”>Hi,
I’m Kevin Yank!</a>

<?php
$firstname=$_GET[‘firstname’];
$firstname=$_GET['lastname '];
echo “welcome ,$firstname ,lastname!”;
?>

i am a beginner again because i quit for along time and unfortunately i forget every thing now when i upload these 2 prog i get t this message

Notice: Undefined index: firstname in C:\wamp\www\welcome2.php on line 2

Notice: Undefined index: lastname in C:\wamp\www\welcome2.php on line 3
welcome , ,lastname!

thank you

If $_GET doesn’t contain anything under ‘firstname’ or ‘lastname’ then it will throw an undefined index notice.

There are a couple of ways of dealing with this. You could assign a default value for the variable, then check if $_GET contains the index you’re lookig before before assigning it:


$firstname = '';
if (!empty($_GET['firstname'])) {
    $firstname = $_GET['firstname'];
}

Or, and this is my favoured solution, you can make use of the filter_input function that is built in to PHP from version 5.2


$firstname = filter_input(INPUT_GET, 'firstname');