Re: UNLESS Statement Equivalent

From: Date: Tue, 12 Mar 2013 00:05:07 +0000
Subject: Re: UNLESS Statement Equivalent
References: 1 2 3 4 5 6  Groups: php.general 
Request: Send a blank email to php-general+get-320478@lists.php.net to get a copy of this message
On 3/11/13 6:25 PM, Angela Barone wrote:
On Mar 11, 2013, at 4:10 PM, Jonathan Sundquist wrote:
the variable $current_page does not exist.
That was my problem. :( I've been staring at this for too long. Too bad there's not a 'use strict' pragma.
There is. Always set your development environment to E_ALL | E_STRICT, and it will yell at you about every little thing, including undefined variables. http://php.net/manual/en/function.error-reporting.php
I would also suggest keeping with your original statement to return early and return often. Its best to exit out of your functions sooner than later. Specially if its a large function.
O.K. I just thought there might be a more elegant way of doing it. I at least got rid of the else statement like you mentioned. Thanks for your help, Angela
If you find yourself with a function that's too long and complex from the if-statements, your first step is to break it up into utility functions. if (...) { // Something Long } else { // Something Else Long } Becomes: if (...) { something_long(); } else { something_else_long(); } function something_long() { } function something_else_long() { } That helps both readability and testability. Also, on your original boolean question, note that negation is distributive. That is: !($a && $b && $c) is the same as: !$a || !$b || $!c Which means that if your checks are all equality checks, as in your case, you can simply do: if ($a != 'a' || $b != 'b' || $c >= 'c') { } Which may be easier to read. If those checks are not trivial then there's also a micro-performance benefit there, as the first statement that evaluates to true will cause the whole thing to return true, so the second and third options don't need to be evaluated. --Larry Garfield

« previous php.general (#320478) next »