php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Uncaught TypeError: Unsupported operand types: string * string in

Open tolgahanacar opened this issue 4 years ago • 15 comments

I'm pulling currency api with json_decode.

I can read the data properly, but it returns problems with multiplication. Sample; ['USD']['sale'] * $value

The following code:

<?php
$value1 = $_POST['value1'] // var_dump: string
$value2 = $_POST['value2'] // var_dump: string or int

echo $value1 * $value2; // example 1 * 3

Resulted in this output:

Uncaught TypeError: Unsupported operand types: string * string in  
// Converting with intval doesn't work, it returns values ​​as 0.

But I expected this output instead:

3

PHP Version

PHP 8.0.1

Operating System

Microsoft Windows 10 Home [10.0.19043]

tolgahanacar avatar Jan 06 '22 12:01 tolgahanacar

One of your operands must not be a numeric string. Can you provide the post parameters you're supplying? Also, PHP 8.0.1 is an old patch, can you try on the latest PHP 8.0.14?

iluuu1994 avatar Jan 06 '22 12:01 iluuu1994

$currency = json_decode(file_get_contents('https://api.example.com/embed/currency.json'), true);

$value = $_POST['value']; // var_dump: string $usd = $currency['USD']['sale']; // var_dump: string

echo $value * $usd; // output error

I also tried with PHP Version 8.1.0.

tolgahanacar avatar Jan 06 '22 12:01 tolgahanacar

I tried this as a solution.

$value = intval($_POST['value']); // var_dump: int $usd = intval($currency['USD']['sale']); // var_dump: int

echo $value * $usd; // output 0

tolgahanacar avatar Jan 06 '22 12:01 tolgahanacar

@tolgahanacar But can you provide the output of the two var_dump calls? The error you're describing usually occurs when one of the operands isn't a valid numeric string. For example:

$foo = 'asdf';
$bar = '3';
echo $foo * $bar;

https://3v4l.org/3jMvJ#v8.0.14

Thus it would be useful to know what these operands are in your case.

iluuu1994 avatar Jan 06 '22 12:01 iluuu1994

@iluuu1994

$foo = 'asdf';
$bar = '3';
echo $foo * $bar;

// PHP: 8.1.0
// Result: Uncaught TypeError: Unsupported operand types: string * string in

// This problem may give output in the values ​​we define, but it may cause problems in operations pulled with json or direct array operations. I've seen this issue on several forum pages, but couldn't find any solution.

tolgahanacar avatar Jan 06 '22 12:01 tolgahanacar

@tolgahanacar

$value = $_POST['value']; // var_dump: string
var_dump($value);
$usd = $currency['USD']['sale']; // var_dump: string
var_dump($usd);

echo $value * $usd;

Can you provide the output of this?

iluuu1994 avatar Jan 06 '22 12:01 iluuu1994

PHP 8.1.0

string(3) "13.6" string(1) "1" Fatal error: Uncaught TypeError: Unsupported operand types: string * string in

tolgahanacar avatar Jan 06 '22 12:01 tolgahanacar

@tolgahanacar You cannot multiply these operands. What result would you expect "USD" * "1" to be? What exactly your application is supposed to do when the operands aren't what you expect them to be, only you can know.

$currency = json_decode(file_get_contents('https://api.example.com/embed/currency.json'), true);
$value = filter_var($_POST['value'], FILTER_VALIDATE_FLOAT);
$usd = filter_var($currency['USD']['sale'], FILTER_VALIDATE_FLOAT);

if ($value === false || $usd === false) {
    // Do something else
}

echo $value * $usd;

iluuu1994 avatar Jan 06 '22 12:01 iluuu1994

I fixed it before your post. string(3) "13.6" string(1) "1" Thank you for your attention.

tolgahanacar avatar Jan 06 '22 12:01 tolgahanacar

Wouldn't it be string(4) "13.6"? (note the 4). Can you re-run the example to make sure this is really the output you get?

iluuu1994 avatar Jan 06 '22 12:01 iluuu1994

$value = $_POST['value'] = // string(1) "1" $usd = $currency['USD']['sale']; // string(91) "13.7606"

tolgahanacar avatar Jan 06 '22 13:01 tolgahanacar

string(91) "13.7606" is not a possible output from var_dump. The first number in the parens shows the length of the string. Please post the unaltered output from var_dump, it doesn't matter if it's 91 characters long.

iluuu1994 avatar Jan 06 '22 13:01 iluuu1994

I will return with a more detailed output. see also; https://askphpquestions.com/2021/07/27/in-php-version-8-0-shows-error-as-php-fatal-error-uncaught-typeerror-unsupported-operand-types-string-string/ https://wordpress.org/support/topic/error-on-php8-unsupported-operand-types-string-float/

tolgahanacar avatar Jan 06 '22 13:01 tolgahanacar

@iluuu1994

Although the api data is actually a numeric value, it is processed like a string expression (it's not about us, it's completely with the person sharing the api), the api processed as int responds to rotation and mathematical operations without any problems.

<8 PHP

Mathematical operations can be done with string expressions.

tolgahanacar avatar Jan 06 '22 13:01 tolgahanacar

@tolgahanacar

<8 PHP Mathematical operations can be done with string expressions.

Yes, before PHP 8 all non-numeric strings in arithmetic operations could get cast to 0. This could lead to detrimental bugs, especially when dealing with currencies like you are. If you look at my comment here, you can use filter_var to convert the value to a number. If the value turns out not to be a number it's converted to false, in which case you can decide what to do yourself (abort, use a default number, whatever is appropriate).

iluuu1994 avatar Jan 06 '22 13:01 iluuu1994