Req #40296 [Com]: "unless" control structure

From: Date: Sun, 29 Jul 2012 20:00:40 +0000
Subject: Req #40296 [Com]: "unless" control structure
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-172315@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=40296&edit=1

 ID:                 40296
 Comment by:         email at philsturgeon dot co dot uk
 Reported by:        mail at tobyinkster dot co dot uk
 Summary:            "unless" control structure
 Status:             Open
 Type:               Feature/Change Request
 Package:            Feature/Change Request
 Operating System:   All
 PHP Version:        5.2.0
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2011-12-30 13:50:47] joke at nakhon dot net

I vote against this request. Both perl and ruby's 'unless' confuse me so much. It can
be easy to abuse.

I often encounter someone wrote code like this:

unless a != 7
  b = 3
else
  b = 6
end

------------------------------------------------------------------------
[2011-08-04 20:59:24] achaia3 at gmail dot com

This would be a convenient feature for sure with no down sides for anybody not wishing to use it.

------------------------------------------------------------------------
[2011-06-05 22:47:45] mattr dot smith at gmail dot com

I also request this feature. 

Ruby has this structure too and really adds to the readability of code.

I don't see any argument at against this that is valid other than the fact that 
PHP programmers don't want to change.

------------------------------------------------------------------------
[2007-12-31 23:19:51] michael at chunkycow dot com dot au

G'day

Perl has lots of 'features' in it, although I cannot see how an 
'unless' control structure adds to anything but confusion for existing/new developers.

------------------------------------------------------------------------
[2007-01-31 12:26:42] 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.



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=40296&edit=1


Thread (19 messages)

« previous php.bugs (#172315) next »