PHP如何在数学运算中绕过字符串?

$str = 'we123'+'123we'+'we'+'123';

echo $str; Gives output 246

$str = 'we123'-'123we'-'we'-'123';

output is -246

$str = 'we123'*'123we'*'we'*'123';

output is 0

I am not getting any warning or notice during this operations.

But In division operation,

$str = 'we123'/'123we'/'we'/'123';

Gives,

Warning: Division by zero

Your help is highly appreciated.

Thanks in advance :)

It is how PHP works, look at PHP string conversion. Quote from this page:

When a string is evaluated in a numeric context, the resulting value and type are determined as follows.

If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float.

The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.

Your strings are evaluated in numeric context, thus:

'we123' - becomes 0, '123we' - becomes 123, 'we' - becomes 0, '123' - becomes 123

Then calculating 0 / 123 / 0 / 123 gives division by zero as expected.