|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-01-31 12:26 UTC] mail at tobyinkster dot co dot uk
Description:
------------
It would be nice if PHP had an "unless" control structure so that:
unless (...) { ... }
would be equivalent to:
if (!(...)) { ... }
Although the "unless" control structure is actually slightly more verbose (in bytes) than "if", it is arguably a lot more readable in the following examples:
unless ($a==0 || $b==0)
divide_by($a*$b);
versus:
if (!($a==0 || $b==0))
divide_by($a*$b);
or, equivalently using De Morgan's Law (Google for it):
if ($a!=0 && $b!=0)
divide_by($a*$b);
The "unless" structure uses less punctuation, and more human syntax. I can't imagine how this extra syntax would break any existing scripts.
For what it's worth, Perl already has an "unless" control structure.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 21:00:01 2025 UTC |
I am surprised to see such a lack of interest on this feature request. Ruby and Perl both have "unless", and while that is certainly not a reason on its own, it does show that this is used. There are two people here saying that unless is confusing. How so? If something matches false then do whatever. It has an else too, so if something is true it will be used. The main point here is not to remove a single !, but to make much cleaner syntax. if ( ! (isset($foo) or ($foo !== 'bar')) exit('invalid value'); or unless (isset($foo) and $foo === 'bar') exit('invalid value'); Its a minor difference as I cannot remember a more compelling example, but there have been numerous times I have wished to reverse this and have unless instead of an extremely complex if statement. Unless just makes sense, it's optional, it doesn't break BC, it makes syntax easier to read in some situations and it is easy to document. If I knew enough C I'd send in a patch right now as PHP would be better off for having this.In regards to double negatives, I agree. If I see a developer do this unless a != 7 then I would block their PR and instantly go and have a talk with them about writing sane code. As for else it really shouldn't be used that much but it should be possible. unless (foo === 'bar') { // do something } else { // do something else } Is that really a confusion? Unlesselse might become a dog though, not sure about that: unless (foo === 'bar') { // do something } unlesselse (foo === 'baz') { // do something } else { // do something else } At that point you'd just want to be using a switch, but that is the same for if's. As I said unless should not really use an else, otherwise you'd be better off just using an if and swapping it around, but having it doesn't hurt.FWIW, while Perl does allow unless (foo) { bar } else { baz } I've never seen it in the wild. I've only ever seen unless used without any trailing else conditions. (And although Perl syntax allows else following unless, it explicitly disallows elsif following unless.) I'd be perfectly happy for PHP to forbid both elseif and else after unless. > It also isn't a very common feature in other languages Latin had "nisi". Modern languages derived from Latin are all the poorer for having lost this concept. > It is an odd word that essentially means not-if even though > it logically should be equivalent to "more" as in the > opposite of "more" would be "less" and sticking "un" in > front of it suddenly completely changes the meaning entirely. By that logic, uniqid() should return the opposite of the iqid() function.