Re: incrementing boolean values
| From: | Derick Rethans | Date: | Sun, 05 Jun 2005 20:27:51 +0000 |
| Subject: | Re: incrementing boolean values | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-16389@lists.php.net to get a copy of this message | ||
On Sun, 5 Jun 2005, Johannes Schlueter wrote:
> Hi,
>
> PHP is a weak-typed language which casts in the background as required - at
> least most of the time. I recently found an exception which bugged me:
>
> $a = false;
> $a++;
>
> Here $a isn't casted to int or "incremented" to true but the incrementing has
> no effect. By checking zend_operators.c I saw that booleans had no explicit
> incrementing rule but uses just the default in the relevant switch.
>
> Looking a bit deeper it got quite interesting: NULL++ gives as result the
> integer one. This is fine for incrementing undefined variables but imho
> inconsistent with the behavior of false. NULL-- evaluates to NULL similar to
> false but different from NULL++.
>
> All this makes using PHP harder than needed. At least I spent quite some time
> finding that my variable was set to false instead of 0 (or NULL).
>
> I wrote the attached patch which allows in-/decrementing of simple types by
> casting bools and NULL to long. Yes, it would be a BC break but I don't think
> someone relies on false++ being false and it would make life simpler.
>
> Comments?
I did this a year or so ago, and after discussing with Andi we decided
not to promote types in this case.
Derick
--
Derick Rethans
http://derickrethans.nl | http://ez.no | http://xdebug.org