Re: UNLESS Statement Equivalent

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux