Page 1 of 1

Variable scope problem?!?!?

Posted: Thu Nov 06, 2008 5:54 am
by yoji
First off, here is the code:

Code: Select all

 
<?php
if (isset($_POST['Submit']))
{
 
    //variables
    $fName=$_POST['fName'];
    $lName=$_POST['lName'];
    $email=$_POST['email'];
    @ $fp=fopen("$dRoot//fopen//data//entries.txt",'ab');
    
    if (!$fp)
    {
            echo "Couldn't open the file";
            exit;
    }
 
    if (!filter_var($email, FILTER_VALIDATE_EMAIL))
    {
        $fError&="Please write a proper email<br />";
        $errorNumber=1;
    }
    
    if(strlen($fName)<6)
    {
        $fError&="Please write a proper email<br />";
        $errorNumber=1;
    }
    if (!isset($errorNumber))
    {
    $fdata=$fName."\t".$lName."\t".$email."\t".date("d-m-Y")."\t".date("H-i-s")."\t"."\r\n";
    fwrite($fp,$fdata,999);
    }
    else
    {
    echo $fError;
    }
}
?>
 
what it does is that first it checks for any error.. If there IS an error it will give $errorNumber=1. Otherwise $errorNumber still won't be initialized and thus it will write the data to a text file.


The problem :


This code doesn't show any erros whatsoever.The problem is with "$fError&=" THAT I know. If I use "$fError=" instead than it works fine, but the problem is that in case of multiple errors it only shows one. Any solutions? Also if I run the code same as it is, it gives a warning saying $fError is undefined. Now if there IS no error than why is php interfering what's inside the if statment?

Re: Variable scope problem?!?!?

Posted: Thu Nov 06, 2008 6:16 am
by papa
$error .=

or save it in an array:

$error[] =

Re: Variable scope problem?!?!?

Posted: Thu Nov 06, 2008 11:10 am
by RobertGonzalez
The ampersand you are using is a reference operator. The context you are using it in is not really correct. Are you trying to capture all errors or are you interested in just one?