Re: [Question] behavior of `null + null = 0`
| From: | Ashley Sheridan | Date: | Mon, 04 Sep 2023 23:07:32 +0000 |
| Subject: | Re: [Question] behavior of `null + null = 0` | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-328272@lists.php.net to get a copy of this message | ||
On 04/09/2023 23:55, JEFFRY KILLEN wrote:
It could also be seen: null == 0 null(0) + null(0) == 0 JKPlease try not to top post. Technically, null == 0 is true, but only because you're using loose typed comparisons, which themselves then fall back to the type comparison table (https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.types ), which attempts to convert it to an empty string or a zero, which is why null == "" as well. Thanks, Ash -- www.ashleysheridan.co.ukOn Sep 4, 2023, at 3:51 PM, Ashley Sheridan <ash@ashleysheridan.co.uk> wrote: On 04/09/2023 05:35, tyamahori wrote:Dear PHP users. I have questions about the behaviorWhat you're looking at here is type coercian, or as PHP refers to it, type juggling. You're using a non-numerica type as a number when you use the + operator. This has the effect of casting null first to an int, which results in your calculation becoming 0 + 0. More details on PHPs type juggling can be found at https://www.php.net/manual/en/language.types.type-juggling.php Thanks, Ash -- www.ashleysheridan.co.uknull + null = 0. Since PHP4.3.0,null + nullbecomes0. https://3v4l.org/YX3Od#veol Form php-src, https://github.com/php/php-src/blob/e2189e5f376092eeaeb4c06ff9b5918bfc016917/Zend/zend_operators.c#L325 It seems _zendi_try_convert_scalar_to_number converts null to 0. Do you know who decided this behavior ? Do you know how did this behavior come about? Regards. Kazuki