I’ll assume you want to know how to get rid of this error message.
The first time you load this page you display a form and $_GET is empty (that’s why it is triggering warnings). Then you submit the form and the fname and age parameters will be added to the url (because your form’s method is ‘get’).
To resolve your issue you could wrap the two lines inside some if-statement, for example:
<?php if(isset($_GET['fname']) && isset($_GET['age'])): ?>
<br/>
Your name is <?php echo $_GET["fname"]; ?>
<br/>
Your age is <?php echo $_GET["age"]; ?>
<?php endif; ?>
Problem :
So, I’m learning PHP and I keep getting “Warning: Undefined array key” in $GET_[“fname”] and $GET_[“age”]:
<main>
<form action="inputs.php" method="get">
Name:
<br/>
<input type="text" name="fname">
<br/>
Age:
<br/>
<input type="number" name="age">
<br/>
<input type="submit" name="submit">
</form>
<br/>
Your name is <?php echo $_GET["fname"]; ?>
<br/>
Your age is <?php echo $_GET["age"]; ?>
</main>
Comments
Comment posted by isset
One the “first call” of the page the parameters are not set, that’s why the error shows up. Check first if they are set using
Comment posted by mintfudge
Thx for the action tip. :))
Comment posted by mintfudge
I’m learning from a youtube course that was recorded in 2018, could be because it was an old version of php? In the video the teacher don’t show any “isset” and his code still works, but mine doesn’t.
Comment posted by brombeer
I doubt that the tutorial code works without warnings/errors. Turn off error reporting/display and errors are not shown (bad practice), but your $_GET parameters will nevertheless
Comment posted by mintfudge
Got it. Thank you so much for the help @brombeer
Comment posted by Joshua Angnoe
Yes, since php 8.0 (latest version) this type of errors are presented as warnings, before this they where classified as notices and notices are often times ignored.