Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 11th, 2003, 07:29 AM
Authorized User
 
Join Date: Jun 2003
Location: Kings Lynn, Norfolk, United Kingdom.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to starsol
Default Returning variables from a function

I'm adding a feature to a script that allows people to block the script from ever sending them an email - and it works fine.

The code to check if an email address is blocked is as follows:

Quote:
quote:
$blocked_file = "members/block_list_file.txt";

$file = fopen($blocked_file,"r");
$list = fread($file, filesize($blocked_file));
fclose($file);
$list=stripslashes($list);
$out="";
$lines = explode("|",$list);
foreach($lines as $l){
        if ($l != $email){
        $out .= "|".$l;
        }
        else {
        $is_blocked = "1";
        }
    }
However, when I try to use it as a function:

Quote:
quote:
function blockcheck($email){

$blocked_file = "members/block_list_file.txt";

$file = fopen($blocked_file,"r");
$list = fread($file, filesize($blocked_file));
fclose($file);
$list=stripslashes($list);
$out="";
$lines = explode("|",$list);
foreach($lines as $l){
        if ($l != $email){
        $out .= "|".$l;
        }
        else {
        $is_blocked = "1";
        }
    }

return $is_blocked;

}
and call it:

Quote:
quote:
blockcheck($email)
$is_blocked is never 1. So I assume that the "return $is_blocked;" is incorrect. I've checked lots of places, and returning variables from functions is always written the same, but for some reason it is not valid in this case. Can anyone give me a pointer as to what I'm doing wrong?

Thanks and very best wishes,
Rupe

Rupe Parnell
www.starsol.co.uk
__________________
Rupe Parnell
www.starsol.co.uk
Reply With Quote
  #2 (permalink)  
Old September 11th, 2003, 12:58 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, this might be stupid questions, but here goes:

Where is $is_blocked NOT 1?
Where and how are you checking it?
What does your function DO, exactly?
Why do you build up the $out string if you're not going to use it?
Why do you set the value of $is_blocked to the string "1" instead of the integer 1, or better yet, the boolean true?


See, your function answers a question: Is this email blocked? That answer is yes or no; boolean values.

Another problem -- you search through the ENTIRE file contents before returning a value. If your blocked email is the first in the list, you'll still waste the time making string comparisons for every other address in the array... why?

Here's a much more efficient rewriting of your function:

function is_blocked($email)
{
    $blocked_file = "members/block_list_file.txt";

    $list = file_get_contents($blocked_file);
    $list = stripslashes($list);

    $lines = explode("|",$list);

    return in_array($email, $lines);
}

http://www.php.net/file_get_contents
http://www.php.net/in_array

See, you pass in an email address. The function tells you if that email address is in the file. Here's another rewriting:

function is_blocked($email)
{
    $blocked_file = "members/block_list_file.txt";

    $list = file_get_contents($blocked_file);
    $list = stripslashes($list);

    return (false !== stripos("|{$email}|", $list));
}

http://www.php.net/stripos

This rewriting is nicer because you don't spend a bunch of time or waste memory splitting the file contents up into an array.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
Reply


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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Returning Automatic Variables from Functions jabney BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 2 November 29th, 2008 09:56 PM
Function returning an array rfstuck Pro VB 6 5 March 12th, 2008 07:35 AM
Function not returning value civa Access 5 January 17th, 2006 03:44 AM
Multi returning function reguengos Pro VB 6 2 March 3rd, 2005 01:48 PM
Function not returning anything... goatboy Beginning PHP 2 December 1st, 2003 12:34 PM



All times are GMT -4. The time now is 10:10 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.