Beginner PHP "Warning: Undefined array key"
When you start working with superglobals like $_GET, it is common to encounter a specific error message that stops many beginners in their tracks: Warning: Undefined array key "fname". This isn't a syntax error or a configuration issue; it is a direct consequence of how PHP handles associative arrays and how HTML forms transmit data.
In the code snippet you provided, the form utilizes the method="get" attribute. When a browser submits this form, it appends the values to the URL query string. However, there is a critical condition here: for a key to exist in $_GET, the field must be present in the request and have been submitted.
Looking closely at your input fields:
<input type="text" name="fname">
<input type="number" name="age">
<input type="submit" name="submit">
Notice that name="submit" is attached to the submit button, but there are no name attributes on the text or number inputs themselves. In HTML5 and many legacy browsers, an input without a name attribute will not send data to the server upon submission. Consequently, when your script runs echo $_GET["fname"], the $_GET array is empty because the browser never included that key in the request parameters. PHP then throws the warning because it attempts to access a non-existent index.
To fix this immediately, you must ensure every input field that needs to be processed has a corresponding name attribute:
<input type="text" name="fname">
<input type="number" name="age">
While adding the name attributes solves the immediate error, it highlights a broader architectural pitfall. Relying on direct array access ($_GET["key"]) is fragile. If you ever refactor your form, remove a field, or encounter an edge case where a user bypasses certain validation steps, this warning will reappear instantly. It also clutters your output with warnings if not handled carefully, potentially obscuring more critical errors like database connection failures later in the script.
The robust approach involves using the isset() function before accessing these keys. This checks whether the key exists in the array and is not null before attempting to read it:
<?php
if (isset($_GET["fname"])) {
echo $_GET["fname"];
} else {
echo "Name was not provided.";
}
?>
Alternatively, you can provide a default value using the coalescing operator (??), which is available in PHP 7.0 and later:
<?php
echo $_GET["fname"] ?? "No name provided";
?>
This pattern ensures your application remains stable even when users navigate directly to inputs.php without submitting the form, or if they fill out only partial information. It shifts the burden of handling missing data from a runtime warning to a controlled logic branch within your code.
If you are planning to move beyond simple scripts into larger applications, this specific issue is often solved by using a framework like Laravel. Laravel provides robust request validation and sanitization layers that prevent these warnings at the application level rather than leaving them to be managed manually in every controller. You can find more about handling requests and input validation with modern frameworks at https://laravelcompany.com.
Ultimately, understanding why $_GET["fname"] fails teaches a fundamental lesson about HTTP semantics: data is only transmitted if explicitly named and submitted. Ensuring your inputs have name attributes and guarding your access to superglobals with isset() or the null coalescing operator are the standard practices that turn fragile scripts into resilient applications.
