TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   return boolean in function scope (http://www.talkphp.com/general/2199-return-boolean-function-scope.html)

Orc 02-06-2008 03:58 PM

return boolean in function scope
 
What does it mean for a functions scope to have a boolean false, when returning the boolean.

Example:
PHP Code:


 
function foo(args ..)
 {
  return 
true;
  if (
$bar)
  {
   echo 
$bar;
  }
  return 
false;
 } 

I look at scripts like this, and it just confuses me as to why would a php programmer put something as return true and false in there? It's only making the file larger. :P

Salathe 02-06-2008 04:26 PM

It makes no sense to write a function exactly like the one you posted, because the function will always return TRUE. However it can be useful to have multiple return points based on different conditions.

PHP Code:

// Useless example
function do_something($with_arg NULL)
{
    if (
$with_arg === NULL)
        return 
FALSE;

    echo 
'Arg is: ';
    
var_dump($arg);
    return 
TRUE;



wGEric 02-06-2008 05:46 PM

I return booleans if the function is suppose to perform an action but isn't required to return data. True and false lets me know if the function was successful or not. It also allows me to not put error handling within the function so that other people can use it easily or I can do whatever I want with the error outside of the function.


All times are GMT. The time now is 11:48 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0