Viewed   6.1k times

So, I'm learning PHP and I keep getting "Warning: Undefined array key" in $GET_["fname"] and $GET_["age"]:


    <form action="inputs.php" method="get">
        <input type="text" name="fname">
        <input type="number" name="age">
        <input type="submit" name="submit">

        Your name is <?php echo $_GET["fname"]; ?>
        Your age is <?php echo $_GET["age"]; ?>




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'])): ?>
        Your name is <?php echo $_GET["fname"]; ?>
        Your age is <?php echo $_GET["age"]; ?>
<?php endif; ?>
Monday, October 31, 2022
    print "we've got records!";
Tuesday, August 30, 2022

The good news is that PHP and JavaScript have a similar idea about what values are true and false.

  • An empty string will be false on both sides. A string with something in it (except 0 in PHP) will be true on both sides.
  • The number 0 will be false on both sides. All other numbers will be true on both sides.

Since the values of a form will always be strings, as Quentin pointed out in his answer, a good practice might be to use an empty string as false value and something else (e.g. 'true') as true value. But I think your way of using 0 and 1 and testing the numerical values is the safest approach because it isn't misleading. (When someone sees 'true' they might think 'false' would also be usable for a false value.

Saturday, August 20, 2022

Works for me. Just tried

<select name="multiselect[]" ...

Results in a URL ?multiselect%5B%5D=2&multiselect%5B%5D=3

which will make $_GET['multiselect'] an array in PHP.

Perhaps you have something else in your system, stripping the [] ?

Saturday, December 17, 2022

The other responses focus on the differences between the two functions. This is true, but if the source array does not contain null or 0 or "", ... (empty values) values you can benchmark the speed of the two functions:


function makeRandomArray( $length ) {
    $array = array();
    for ($i = 0; $i < $length; $i++) {
        $array[$i] = rand(1, $length);

    return $array;

function benchmark( $count, $function ) {
    $start = microtime(true);
    for ($i = 0; $i < $count; $i++) {
    return microtime(true) - $start;

$runs = 100000;
$smallLength = 10;
$small = makeRandomArray($smallLength);

var_dump(benchmark($runs, function() {
    global $small, $smallLength;
    array_key_exists(rand(0, $smallLength), $small);
var_dump(benchmark($runs, function() {
    global $small, $smallLength;
    !empty($small[rand(0, $smallLength)]);

Which gave me the following results:

For a small array:

  • array_key_exists: float(0.18357992172241)
  • empty: float(0.072798013687134)
  • isset: float(0.070242881774902)

For a relative big array:

  • array_key_exists: float(0.57489585876465)
  • empty: float(0.0068421363830566)
  • isset: float(0.0069410800933838)

So if it's possible it's faster to use empty or isset.

Saturday, November 19, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :