Variable scope problem?!?!?

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
yoji
Forum Commoner
Posts: 25
Joined: Sun Oct 19, 2008 3:09 am

Variable scope problem?!?!?

Post 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?
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Variable scope problem?!?!?

Post by papa »

$error .=

or save it in an array:

$error[] =
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Variable scope problem?!?!?

Post 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?
Post Reply