returning values from function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

returning values from function

Post by shivam0101 »

which is the right way of returning values.

Code: Select all

function ReadTemplate($file)
 {
    if(!empty($file) && file_exists($file))
    {
      $fh=fopen($file, 'r');
      $file_read=fread($fh, filesize($file));
      fclose($fh);
      return $file_read;
    }
   else
    {
      return 'Error in reading file';
    }
 }

OR

Code: Select all

function ReadTemplate($file)
 {
    if(!empty($file) && file_exists($file))
    {
      $fh=fopen($file, 'r');
      $file_read=fread($fh, filesize($file));
      fclose($fh);
      $return= $file_read;
    }
     else
     {
      $return= 'Error in reading file';
     }

     return $return;
 }
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

There is no right way. Whichever way you feel comfortable doing. Personally, I'd prefer the first example.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: returning values from function

Post by superdezign »

By standards, you wouldn't have an else statement there at all, so it'd be the first way, just without the 'else' branch.

It'd make more sense to return false or NULL instead of an error message, though.
jeffery
Forum Contributor
Posts: 105
Joined: Mon Apr 03, 2006 3:13 am
Location: Melbourne, Australia
Contact:

Post by jeffery »

I normally do it this way:

Code: Select all

function ReadTemplate($file)
 {
    $file_read = '';
    if(!empty($file) && file_exists($file))
    {
      $fh=fopen($file, 'r');
      $file_read=fread($fh, filesize($file));
      fclose($fh);
    }

    return $file_read;
 }
fly135
Forum Newbie
Posts: 3
Joined: Sat Sep 15, 2007 10:32 pm

Post by fly135 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


how about this

Code: Select all

function ReadTemplate($file)
 {
    if(!empty($file) && file_exists($file))
    {
      $fh=fopen($file, 'r');
      $file_read=fread($fh, filesize($file));
      fclose($fh);
      return $file_read;
    }
   
      return 'Error in reading file';
    
 }

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Like the others said i would rather return False instead of returning an error message itself, then you can't use it like..

Code: Select all

if(!ReadTemplate($file)){
  // Stuff
}
Because it will always return True.
Post Reply