Re: [PHP] php programming style
It depends on many issues...do you have a set standard that you use when programming. 1. does it make sense to do it that way. 2. are there performance issue 3. is it understandable for maintenance by yourself or others. 4. does the function require a return value, if so which type. I think that some of the above question might help you out. However, from a programming standpoint i like to make sure that my functions return the same type at least. Some feel different about this and return various type (ie, int, strings, arrays)... HTH On Thu, 2002-11-21 at 10:57, Beau Hartshorne wrote: Hi, I'm curious if it's bad coding style for a function to return a value on success, or simply false on fail. Here's what I mean: ?php function foo($number) { if (is_numeric($number)) { return $number . ' is a number.'; } else { return false; } } if ($string = foo(1)) { echo $string; } else { echo 'error'; } ? Is this ok? Thank you, Beau -- Ray Hunter email: [EMAIL PROTECTED] www:http://venticon.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] php programming style
I think that some of the above question might help you out. However, from a programming standpoint i like to make sure that my functions return the same type at least. Some feel different about this and return various type (ie, int, strings, arrays)... I agree that it should return a consistent type. I was planning on returning a populated array on success, or a boolean false on fail. Instead, I will return an empty (but initialized) array on fail, and use the count() function to test for failure like this: ?php function foo() { /* if the result set is empty */ return array(); } $my_array = foo(); if (count($my_array) = 0) { // nothing in array echo 'nothing'; } else { // do something with data echo 'something'; } ? Beau -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] php programming style
At 19:54 21.11.2002, Beau Hartshorne said: [snip] I agree that it should return a consistent type. I was planning on returning a populated array on success, or a boolean false on fail. Instead, I will return an empty (but initialized) array on fail, and use the count() function to test for failure like this: [snip] I also have the habit of always having my functions returning a comparable type. Comparable means you can always use the empty(), isset(), or count() functions with correct result. This said I'd suggest to return null for non-void functions in case of failure. -- O Ernest E. Vogelsinger (\)ICQ #13394035 ^ http://www.vogelsinger.at/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php