TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-06-2008, 03:58 PM   #1 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default 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
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 02-06-2008, 04:26 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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;

Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Orc (02-06-2008)
Old 02-06-2008, 05:46 PM   #3 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

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.
__________________
Eric
wGEric is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:50 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design