RE: [PHP-DEV] Little switch improvement: get the used variable
| From: | Christian Stoller | Date: | Thu, 25 Sep 2014 10:54:11 +0000 |
| Subject: | RE: [PHP-DEV] Little switch improvement: get the used variable | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-77622@lists.php.net to get a copy of this message | ||
From: Rowan Collins [mailto:rowan.collins@gmail.com], Sent: Thursday, September 25, 2014 12:31 PM
>
> Sorry, I was talking about this bit:
>
>> Currently, switch always uses a "loose" comparison (==), so cannot
>> distinguish between "case 3" and "case 3.0". Occasionally, it would be
>> nice to switch on a strict comparison (===); but then I thought, why
>> stop there? What if you could use switch with any comparison operator?
>>
>> My idea is to give the switch() statement an optional "use" clause
>> (since that's an existing keyword) containing a single comparison operator
>
> See my earlier e-mail for examples and further details. Maybe I should
> have given it its own thread so it was more visible; the idea has been
> brewing for a while, I just thought this thread was a convenient place
> to mention.
Ah, okay, sorry.
What do you think about that:
$value = 3;
switch (true) {
case 3.0 === $value: break;
case 3 < $value: break;
case 3 > $value: break;
}
Christian