0

I'm currently learning PHP from the very basic and started following a 2 year old youtube video. I'm trying to make a simple php calculator based from his code, but I've run into an error despite copying 100% of the example code, I'm guessing this is due to the version difference. What can be done to fix this issue, error code says undefined array, does this mean that I just need to pre-define the variables? How would I do this.

    <!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>

    <form action="site.php" method="get">
      <input type="number" name="num1">
      <br>
      <input type="number" name="num2">
      <br>
      <input type="submit">
    </form>

  Answer: <?php echo $_GET["num1"] + $_GET["num2"] ?>

  </body>
</html>
4
  • @RGriffiths yes, that's the exact code he was using as an example, and it was working in his video. use dot like this? Answer: <?php echo $_GET["num1"] . $_GET["num2"] ?> it gives the same error Commented Jan 8, 2021 at 21:36
  • Forget the dot thing. name = "num1", name = "num2" you have missed the equals Commented Jan 8, 2021 at 21:41
  • @RGriffiths Hi thank you very much for pointing that out, I did miss the = and that was very dumb of me. The code is working now, but the error still persist. ERROR: Warning: Undefined array key "num1" in C:\Users\Kenji\www\site.php on line 17 Commented Jan 8, 2021 at 21:47
  • You're not checking to see if the form has been submitted. If it hasn't the $_GET array will be empty and you'll get the error you're seeing. The $_GET variables will only exist when the form is actually submitted. Commented Jan 8, 2021 at 21:58

1 Answer 1

1

Explanation:

In the past (old php versions) it was not necessary to define/declare variables. This has changed and now you will get warnings/notices.

If you call the page for the first time the variables num1 and num2 are not defined - that will result in that warning. Only when you enter them and send the form then they will have a value.

So insert a condition if that variables are empty and only if they are NOT empty make the calculation and print it.

! means NOT

isset() checks if the variable exists. https://www.php.net/manual/de/function.isset (empty was a wrong approach because 0 is a valid entry as one commenter pointed thankfully out)

 <!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>

    <form action="site.php" method="get">
      <input type="number" name="num1">
      <br>
      <input type="number" name="num2">
      <br>
      <input type="submit">
    </form>

  Answer: <?php 

if ((!isset($_GET["num1"]) && (!isset($_GET["num1"])){ 
    echo $_GET["num1"] + $_GET["num2"]; 
}

?>

  </body>
</html>
Sign up to request clarification or add additional context in comments.

3 Comments

Warning: since you're dealing with inputs of type number, you might want to use isset() rather than !empty() (or a combination of both). empty($_GET["num1"]) will return true if the entered value is zero.
@rickdenhaan you are right - I never had the situation before that 0 may be a valid entry ;-) but yes 0+5 is a valid entry
@rickdenhaan i updated the answer to reflect your comment. thx.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.