Re: RFC: Strict comparisons
| From: | Rowan Collins | Date: | Sat, 24 Sep 2016 11:49:23 +0000 |
| Subject: | Re: RFC: Strict comparisons | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-96130@lists.php.net to get a copy of this message | ||
On 23 September 2016 23:14:37 BST, Dan Ackroyd <danack@basereality.com> wrote:
>On 23 September 2016 at 17:40, Rowan Collins <rowan.collins@gmail.com>
>wrote:
>>
>> I've been pondering an idea for ages of a generalised syntax for
>switch to
>> specify an operator to use, so you'd do something like this:
>
>That's technically possible already:
>
>function foobar($foo)
>{
> switch (true) {
> case $foo === '1':
Yeah, switch(true) has always felt like an ugly hack to me. If I ever do write up my idea I'll
cover this in the RFC, but the main problem is that you have to repeat the "$foo ===" part
in every case, which means a reader can't assume all the cases are of the same form, and in
many situations you're better off just using elseifs.
A switch(true) makes more sense if you have a bunch of basically unrelated conditions with the same
outcome, at which point a bunch of case labels is possibly easier to maintain than a big chain of
boolean ors.
Regards,
--
Rowan Collins
[IMSoP]