Re: [PHP] UNLESS Statement Equivalent

[email protected] (Ashley Sheridan)
Newsgroups php.general
Message-ID <[email protected]>
On Mon, 2013-03-11 at 16:38 -0500, Jonathan Sundquist wrote:

> Since you already have the return statement with the if statement the else
> isn't required. If those three statements are true you would exit the call
> any ways
> On Mar 11, 2013 4:33 PM, "Angela Barone" <[email protected]>
> wrote:
> 
> >         I'm looking for an 'unless' statement, but as far as I can tell,
> > PHP doesn't have one.  Hopefully someone can help me rewrite my statement.
> >
> >         In English, I want to say: "always do something UNLESS these 3
> > conditions are met".
> >
> >         The best I've been able to come up with in PHP is this:
> >
> > if ( ($current_page == $saved_page) and ($current_ip == $saved_ip) and
> > ($current_dt < ($saved_dt + 3600)) ) {
> >         return;
> > } else {
> >         $query  = "UPDATE `table` SET `hits` = '$count', `agent` =
> > '$agent', `ts` = '$date_time'  WHERE `page` = '$page'";
> >         $result = mysql_query($query) or die ('Error! -- ' .
> > mysql_error());
> > }
> >
> >         However, I've read where this is not really acceptable.  Can
> > someone help me eliminate the 'else' portion of this if statement?
> >
> > Thank you,
> > Angela
> >
> > P.S.  I realize the above isn't complete code but it should still be
> > clear.  If not, let me know.
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >


What about this:

if ( !( ($current_page == $saved_page) and ($current_ip == $saved_ip)
and ($current_dt < ($saved_dt + 3600)) ) )

The 3 sub-expressions are grouped with an extra set of brackets and
the ! inverts the whole thing, so no need for an else clause.

Thanks,
Ash
http://www.ashleysheridan.co.uk
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.