Re: UNLESS Statement Equivalent

From: Date: Mon, 11 Mar 2013 22:43:43 +0000
Subject: Re: UNLESS Statement Equivalent
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-320472@lists.php.net to get a copy of this message
On 11-3-2013 22:32, Angela Barone 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.
Logically, unless means the same as if not. As long as you condense your entire expression to a single boolean result, the only thing you would need to do is negate it to create your unless-statement. In code, this would mean: unless ( $a ) = if ( !$a ) or to show it in a more clear way: unless ( $a and $b ) = if ( ! ($a and $b) ) So in simple terms, just stick a ! (or the keyword not) in front of your expression, and you'll have exactly what you want: if( not ( ($current_page == $saved_page) and ($current_ip == $saved_ip) and ($current_dt < ($saved_dt + 3600) ) ) {

« previous php.general (#320472) next »