-2

Im having a problem doing a math operation in php, returning this error:

Warning: Undefined array key "valor1" in C:\xampp\htdocs\provaphp\pages\aula_sobreNos.php on line 17

Warning: Undefined array key "valor2" in C:\xampp\htdocs\provaphp\pages\aula_sobreNos.php on line 18

Warning: Undefined array key "operador1" in C:\xampp\htdocs\provaphp\pages\aula_sobreNos.php on line 19

This is the code:

 <form method="get">
    <input type="hidden" name="p" value="sobreNos">
    <input type="number" name="valor1">
    <input type="text" name="operador1">
    <input type="number" name="valor2">        
    <input type="submit">
</form>

php part:

 $valor1 = $_GET["valor1"];
$valor2 = $_GET["valor2"];
$operador1 = $_GET["operador1"];

$resultado = mathOp($operador1, $valor1, $valor2);
echo $resultado;

function mathOp($operador1, $valor1, $valor2) {
    switch($operador1) {
        case '+':
            return($valor1 + $valor2);
        case '-':
            return($valor1 - $valor2);
        case '*':
            return($valor1 * $valor2);
        case '/':
            if($valor2 == 0) {
                return 'Não pode ser dividido por 0!';
            } else {
                return($valor1 / $valor2);
            }
    }
}

Thanks for your responses!

Paul T.
  • 4,703
  • 11
  • 25
  • 29
  • Not clear why so many claim warnings are errors? ... Anyway, wrap all the code _before_ the function into a `if (!empty($_GET)) { ... code here ... }` condition. The deal is that when the page first loads, the `$_GET` array has not yet been populated, and _none_ of those array keys are set until the form submit. – Paul T. Sep 23 '22 at 02:46
  • Thanks! It worked putting if(!isset($_GET)) before the code. – Bruno Ricardo Carmena Sep 23 '22 at 02:55
  • Now the math function is not working anymore – Bruno Ricardo Carmena Sep 23 '22 at 02:59
  • Update the question with the latest code change. The function should have been left _outside_ of the condition mentioned earlier. (could append as an updated code block) – Paul T. Sep 23 '22 at 03:02
  • Run this [example fiddle](https://phpize.online/sql/mysql57/undefined/php/php81/2ad71164c95184bb08d23771c5e6a027/) without any changes to see no output, and then un-comment lines 3, 4, and 5, and then run it again to see that it works. Consider adding a `default` in the switch that might simply return the message: `"No action for $operador1."` – Paul T. Sep 23 '22 at 03:14

1 Answers1

0

If the system cannot find the correct value for assigning to a variable, then it will prompt warnings like Undefined xxxx "variable_name" in xxxxx

Hence, please

  1. initialize the variables;
  2. use isset to check whether the $_GET values exist when they are assigned to the variables

So, change

$valor1 = $_GET["valor1"];
$valor2 = $_GET["valor2"];
$operador1 = $_GET["operador1"];

to

$valor1 = 0;
$valor2 = 0;
$operador1 = "+";


if (isset($_GET["valor1"])){
$valor1 = $_GET["valor1"];
}

if (isset($_GET["valor2"])){
$valor2 = $_GET["valor2"];
}

if (isset($_GET["operador1"])){
$operador1 = $_GET["operador1"];
}

So , the final code (well-tested) is:

 <form method="get">
    <input type="hidden" name="p" value="sobreNos">
    <input type="number" name="valor1">
    <input type="text" name="operador1">
    <input type="number" name="valor2">        
    <input type="submit">
</form>

<?php

$valor1 = 0;
$valor2 = 0;
$operador1 = "+";


if (isset($_GET["valor1"])){
$valor1 = $_GET["valor1"];
}

if (isset($_GET["valor2"])){
$valor2 = $_GET["valor2"];
}

if (isset($_GET["operador1"])){
$operador1 = $_GET["operador1"];
}
$resultado = mathOp($operador1, $valor1, $valor2);
echo $resultado;

function mathOp($operador1, $valor1, $valor2) {
    switch($operador1) {
        case '+':
            return($valor1 + $valor2);
        case '-':
            return($valor1 - $valor2);
        case '*':
            return($valor1 * $valor2);
        case '/':
            if($valor2 == 0) {
                return 'Não pode ser dividido por 0!';
            } else {
                return($valor1 / $valor2);
            }
    }
}

?>
Ken Lee
  • 6,985
  • 3
  • 10
  • 29
  • Now it gives a warning because the variable is not defined: Warning: Undefined variable $operador1 in C:\xampp\htdocs\provaphp\pages\aula_sobreNos.php on line 31 Warning: Undefined variable $valor1 in C:\xampp\htdocs\provaphp\pages\aula_sobreNos.php on line 31 Warning: Undefined variable $valor2 in C:\xampp\htdocs\provaphp\pages\aula_sobreNos.php on line 31 Is there any form to put a default variable to prevent this? – Bruno Ricardo Carmena Sep 23 '22 at 03:04
  • See my revised answer (three lines added) - well tested and the code works – Ken Lee Sep 23 '22 at 03:50