I’m wondering if there is a particular version, such as PHP 8, where this will cause an error by default:

Multiplying numbers by strings seems like it would be a no-no, although it’s so far worked in PHP for years and years.
There isn’t a no-no there. PHP allows it by design. You’ll have to redesign the language to stop supporting that behavior. See https://www.php.net/manual/en/language.types.type-juggling.php
The main reason it’s allowed, I think, is because web forms submit such values as strings. Or rather, PHP does not do any special format detection on user inputs, they are all strings or arrays of strings. An <input type="number" value="5"> still gets received as the string “5” and not an integer. And PHP is geared towards easily accepting and manipulating user input.
To change that would satisfy an urge to be technically correct, a more robust language for type safety, but would also break an untold number of applications that are working just fine mixing types.
The only error I faced when coming from 7.4 was stringable interface error. I had some cases where it returned ?string before. Now it always needs to be a string.
That’s valid and there’s no plans to change it.

What has changed is whether 1.23 * “99 red balloons” will be interpreted as 1.23 * 99. Prior to PHP 8, it was. As of PHP 8, it’s an error.
A numeric string can get used as a number just fine by PHP and no one has suggested changing that. The problem has always been sorta-numeric strings, like ” 5 “, “99 bottles of beer”, “the 13th warrior”, etc. Those are no-nos, because the logic around them was a mess.
The logic in PHP 8.0 is less of a mess; a numeric string is now “contains only numeric values, and possibly leading or trailing whitespace that gets ignored.” As long as you stick to those, you should be fine.
Caveat: That doesn’t apply at function boundaries. If you pass “4.5” to a function that wants a float in loose types mode, it will cast it to 4.5. If you use strict mode, it will error out. Your code should be in strict mode 99.5% of the time. But within a function what you’re doing is fine.
It returns 5.79 in PHP 7.0, 7.1, 7.2, 7.3, 7.4, and 8.0:
https://phpsandbox.io/n/wild-wind-zjvz
Edit: Oops, that was for plus, multiplying gives 5.6088 in all of them too:
https://phpsandbox.io/n/noisy-voice-gyg3
Oh, I’m surprised, thank you. I wonder if there is any plan to later make that cause a type mis-match error.
If you add them. If you multiply them it returns 5.6088 in all versions: https://3v4l.org/Jaq3Q
username checks out
Never.
“a no-no” is a matter of a preference. There is a whole class of languages doing it this way. If you don’t like it, then PHP is probably not your language of choice.
Multiplying numbers and strings is not a matter of preference. While I love PHP, this thing is a definite “no no”.
I don’t mind it. I’m just wary of it being changed in future.
Currently, my code relies on this behavior, particularly when multiplying values that are being retrieved from a db.

PHPStan can find such mistakes. Since it is valid PHP, you do have to configure it to disallow implicit conversions though.
it’s not a mistake. and no static code analyzer will tell you which type has been returned from a database. and, frankly, it’s none of its business
Members
Online

source