Fatal Error: Unsupported Operand Types

How to fix Fatal error: Uncaught TypeError: Unsupported operand types: string + string [PHP] [HTML]

to fix the current implementation

  • you need to add the switch statements inside the try block,
  • so when the validation exception fire it will escape running it
  • and about showing the error beside the input you can set the exception message in a variable and print it beside the input " ,

your implementation after those edits will looks like :

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
</head>

<?php

$number1 = "";
$number2 = "";
$result = "";
$calculate = "";
$number1Error = '';

if (isset($_GET['calculate'])) {
$number1 = $_GET['number1'];
$number2 = $_GET['number2'];
$calculate = $_GET['calculate'];
$errors = [];

function isValidNumber1($number1)
{
if (!is_numeric($number1)) {
throw new Exception("Number 1 is not a number!");
}
}


try {
isValidNumber1($number1);
$result = $number1 + $number2;

switch ($calculate) {
case "Add":
var_dump($number1);
$result = $number1 + $number2;
break;
case "Substract":
$result = $number1 - $number2;
break;
case "Multiply":
$result = $number1 * $number2;
break;
case "Divide":
$result = $number1 / $number2;
break;
case "Modulo":
$result = $number1 % $number2;
break;
}

} catch (Exception $e) {
$number1Error = $e->getMessage() . PHP_EOL;

}


}

?>

<body>
<h1>Calculator</h1>

<form action="" method="get" id="calculator">

<label for="number1">Number 1: </label>
<input type="text" name="number1" value="<?php echo $number1; ?>"> <?php echo $number1Error; ?><br><br>
<label for=" number2">Number 2:</label>
<input type="text" name="number2" value="<?php echo $number2; ?>"><br><br>

<?php
echo "<p>Operation: $calculate</p>";
echo "<p>Result: $result</p>";
?>

<div>
<input type="submit" name="calculate" value="Add">
<input type="submit" name="calculate" value="Substract">
<input type="submit" name="calculate" value="Multiply">
<input type="submit" name="calculate" value="Divide">
<input type="submit" name="calculate" value="Modulo">
</div>
</form>

</body>

</html>

Fatal error: Uncaught TypeError: Unsupported operand types: string * string

$row['price'] and $_POST['quantity' . $iterate] return strings which can not be multiplied. So you have to convert both to int values:

$subt = (int)$row['price']* (int)$_POST['quantity' . $iterate]

PHP Fatal error: Uncaught Error: Unsupported operand types when adding values to array

That is because $supergroupnew is not defined inside the function and the actual error would read:

Notice: Undefined variable: supergroupnew

You need to make the following small change:

array_walk_recursive($supergroup, function($item, $key) use($id, &$supergroupnew) {
// removed & from id
// added &$supergroupnew to use statement, you can access/modify this external variable

Having said that, using += on arrays will create a union instead of appending. Quote:

The + operator returns the right-hand array appended to the left-hand
array; for keys that exist in both arrays, the elements from the
left-hand array will be used, and the matching elements from the
right-hand array will be ignored.

You should use [] instead unless there is something I don't understand.



Related Topics



Leave a reply



Submit