float casting *= equations in php

Viewed 26

Currently using PHP 8.1, I hit fatal errors whenever I do a formulae such as String * Float. Ok, so I just fix it by float casting it, and it's solved.

But I noticed another issue, is that float casting doesn't work when we use symbols such as *= and +=

Refer below example

$a = "";
$b = "2";
//first way
(float) $a *= (float) $b; //fatal error, unsupported operand String * float
//second way
$a = (float) $a * (float) $b; //works

I assume that the first clause results to

(float) $a = $a * (float) $b;

which causes the error.

Is there anyway to solve this or must it always be done the second way? Assuming I can't control what is $a (I can't float cast it in advance).

Thanks

0 Answers
Related