Re: changing NULL behavior in PHP arithmetic
| From: | Ashley Sheridan | Date: | Thu, 15 Apr 2010 08:08:21 +0000 |
| Subject: | Re: changing NULL behavior in PHP arithmetic | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-304106@lists.php.net to get a copy of this message | ||
On Thu, 2010-04-15 at 09:46 +0200, cr.vegelin@gmail.com wrote:
> Hi All,
>
> Is there an option in PHP to change the behavior of NULL in PHP functions ?
> Now PHP uses NULL as a 0 (zero) for arithmetic, for example:
> NULL + 6 = 6
> NULL * 6 = 0
> NULL / 6 = 0
> 6 / NULL = Division by zero
>
> What I need is the same behavior as #N/A (or =NA()) in Excel, where:
> #N/A + 6 = #N/A
> #N/A * 6 = #N/A
> #N/A / 6 = #N/A
> 6 / #N/A = #N/A
>
> because arithmetic operations with "Unknown" operands should result to
> "Unknown" ...
>
> TIA, Cor
You can't really, because PHP is a loosely typed language, which means
it silently converts values as required by the situation. When you use
mathematical operators, PHP converts the values to numbers, and NULL
maps to a 0 (as does the boolean false and an empty string)
The only way I can see to fix your problem is to check the value of the
variables you are working on with something like is_int()
Thanks,
Ash
http://www.ashleysheridan.co.uk