Re: incrementing boolean values
| From: | Marcus Boerger | Date: | Mon, 06 Jun 2005 06:37:13 +0000 |
| Subject: | Re: incrementing boolean values | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-16400@lists.php.net to get a copy of this message | ||
Hello Derick,
Sunday, June 5, 2005, 10:27:51 PM, you wrote:
> 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.
Not promoting types would mean adding false++, and true-- doesn't it?
And again not doing this complicaates php needlessly. For example the two
snippets below are different, tell me why:
1) if (($p=strpos(...)+1) > 0) ...
2) $p = strpos(...);
$p++;
if ($p > 0) ...
Sidenote: Why is the following a parser error:
if (($p=strpos(...))++ > 0) ...
--
Best regards,
Marcus mailto:mail@marcus-boerger.de