Reputation: 257
I am working on a shopping cart function for a website and have stumbled across this error:
Fatal error: Unsupported operand types in ... on line xx
I think this may be because I am performing some math between a variable and a value within an array. What I am not sure of is how to perform math on a value within an array:
$line_cost = $price * $quantity;
Can anyone give me any guidance on this please? I will be most grateful! Here is the relevant code -
<?php session_start(); ?>
<?php
$product_id = $_GET['id'];
$action = $_GET['action'];
switch($action) {
case "add":
$_SESSION['cart'][$product_id]++;
break;
}
?>
<?php
foreach($_SESSION['cart'] as $product_id => $quantity) {
list($name, $description, $price) = getProductInfo($product_id);
echo "$price"; // 20
var_dump($quantity); // "array(2) { ["productid"]=> string(1) "2" ["qty"]=> int(1) }".
$line_cost = $price * $quantity; //Fatal error occurs here
}
?>
Upvotes: 18
Views: 130153
Reputation: 6701
This error means that some of your data cannot be used in the expression.
For example, you cannot multiply strings. echo "five" * 5;
will result in such an error.
However, PHP isn't that strict toward formal types. It wouldn't complain about particular type as long as the contents looks like a number: you can use "42" in math operations all right.
Now to your particular case. With $quantity
being an array, you won't get any sensible result from this multiplication. So the error message says: "Look at your data. Some of it is not what you think it is!". Go take a look, var_dump()
your operands, find out that $quantity
is an array and then determine, which actual element you need. And then use that element in your equation:
$line_cost = $price * $quantity['qty'];
The same goes for all other cases when you get this error. var_dump()
your data and find out, why don't you get a correct value.
Note that you should NEVER add a mindless type casting to fix this error, as it's suggested in many other answers. Because such casting will silence the error message that helped you so much. If you do something like (int)$quantity['qty'] here, the next time, when $quantity['qty']
in turn won't contain a correct number, PHP won't be able to help you, and would just silently return 0 instead. Making the final cost also zero.
Still this error can appear if you don't have control over input, for example it's coming from the client. Then in order to fix this error you shouldn't mindlessly cast it either. But validate it instead and stop processing in case it doesn't contain the required value.
Upvotes: 34
Reputation: 13561
I think this may be because I am performing some math between a variable and a value within an array.
Not quite.
You are attempting to perform math (multiplication) between an integer and an array, eg: 20 x array. This doesn’t work because array’s don’t have a multiplication operand (and if they did, it probably wouldn’t do what you want).
What you want to do is to perform math on an variable and a value (element) within an array. Since your array is an associative array, you need to supply the key, like so:
$line_cost = $price * $quantity['qty'];
Upvotes: 2