Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 11th, 2003, 07:29 AM
Authorized User
 
Join Date: Jun 2003
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
 
Old September 11th, 2003, 12:58 PM
Friend of Wrox
 
Join Date: Jun 2003
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/





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.