changing NULL behavior in PHP arithmetic

------=_NextPart_000_005A_01CADC80.878315C0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

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 =3D 6
NULL * 6 =3D 0
NULL / 6 =3D 0
6 / NULL =3D Division by zero

What I need is the same behavior as #N/A (or =3DNA()) in Excel, where:
#N/A + 6 =3D #N/A
#N/A * 6 =3D #N/A
#N/A / 6 =3D #N/A
6 / #N/A =3D #N/A

because arithmetic operations with "Unknown" operands should result to =
"Unknown" ...

TIA, Cor

------=_NextPart_000_005A_01CADC80.878315C0--
cr.vegelin [ Do, 15 April 2010 09:46 ] [ ID #2039058 ]

Re: changing NULL behavior in PHP arithmetic

--=-9RPvb9PbS0r8yE5eU1Gn
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Thu, 2010-04-15 at 09:46 +0200, cr.vegelin [at] 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



--=-9RPvb9PbS0r8yE5eU1Gn--
Ashley Sheridan [ Do, 15 April 2010 10:08 ] [ ID #2039059 ]

Re: changing NULL behavior in PHP arithmetic

>> because arithmetic operations with "Unknown" operands should result to "Unknown" ...
in PHP "Unknown" values are represented by NaN, not NULL
http://php.net/manual/en/function.is-nan.php

but what surprises me is
is_nan(6/0) = (bool)false (along with a warning)

>> Now PHP uses NULL as a 0 (zero) for arithmetic
I dont expect anything different, because intval(null) is 0.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
kranthi [ Do, 15 April 2010 10:47 ] [ ID #2039061 ]

Re: changing NULL behavior in PHP arithmetic

------=_NextPart_000_0091_01CADC90.9D3F67A0
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable

From: Ashley Sheridan
To: cr.vegelin [at] gmail.com
Cc: php-general [at] lists.php.net
Sent: Thursday, April 15, 2010 10:08 AM
Subject: Re: [PHP] changing NULL behavior in PHP arithmetic


On Thu, 2010-04-15 at 09:46 +0200, cr.vegelin [at] 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 =3D 6
NULL * 6 =3D 0
NULL / 6 =3D 0
6 / NULL =3D Division by zero

What I need is the same behavior as #N/A (or =3DNA()) in Excel, where:
#N/A + 6 =3D #N/A
#N/A * 6 =3D #N/A
#N/A / 6 =3D #N/A
6 / #N/A =3D #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




Thanks for replying.
I tried the predefined PHP constant NAN.
However, NAN + 6 =3D 6, so NAN is can't be used either.

To bypass the problem, I now use is_null().
is_int() can also be used, but does it have advantages over is_null() ?

Thanks, Cor
------=_NextPart_000_0091_01CADC90.9D3F67A0--
cr.vegelin [ Do, 15 April 2010 11:41 ] [ ID #2039062 ]

Re: changing NULL behavior in PHP arithmetic

----- Original Message -----
From: "Shawn McKenzie" <nospam [at] mckenzies.net>
To: <cr.vegelin [at] gmail.com>
Cc: <php-general [at] lists.php.net>
Sent: Saturday, April 17, 2010 3:41 AM
Subject: Re: changing NULL behavior in PHP arithmetic


> On 04/15/2010 02:46 AM, cr.vegelin [at] 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
>>
>
> In what cases do you have a null var?
>

Hi Shawn,

I am dealing with time series.
As an example, assume rows per year with 12 monthly values.
For 2009 all values are known, and numeric.
For 2010 some values are known, some are unknown.
The 2009 total can be calculated, but the 2010 total should be unknown,
and should not be the sum of the known values.

Thanks, Cor






--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
cr.vegelin [ Sa, 17 April 2010 07:27 ] [ ID #2039247 ]

Re: changing NULL behavior in PHP arithmetic

On 04/15/2010 02:46 AM, cr.vegelin [at] 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
>

In what cases do you have a null var?

--
Thanks!
-Shawn
http://www.spidean.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Shawn McKenzie [ Sa, 17 April 2010 03:41 ] [ ID #2039300 ]
PHP » gmane.comp.php.general » changing NULL behavior in PHP arithmetic

Vorheriges Thema: limit to var_dump?
Nächstes Thema: Include security?