user1510389
user1510389

Reputation: 61

PHP Return Multiple Functions

I am new to PHP, so I apologize if this looks like a mess... I am trying to validate a form using the following three functions - checkName, checkEmail, and checkMessage. The problem I am running into is when I submit the form, it always displays the first error, even if the input is correct. Can anyone tell me what I'm doing wrong?

function checkName(){

    if($name == ''){
        print "Please enter your name!<br />";
        return false;
    }
    else{
        if(strlen($name)<2) {
            print "Your name should be more than 1 characters long!<br />";
            return false;
        }
        else{
            return true;
        }
    }
}
function checkEmail(){

    if($from == '') {
        print "Please enter your email address!<br />";
        return false;
    } 
    else{
        if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $from)){
            print "Please enter a valid email address!<br />";
            return false;
        }
        else{
            return true;
        }
    }
}
function checkMessage(){

    if($message == '') {
        print "Please enter your message!<br />";
        return false;
    }
    else{
        if(strlen($message)<10) {
            print "Your message should be more than 10 characters long!<br />";
            return false;
        }
        else{
            return true;
        }
    }
}

if($validation == ''){

    $a = checkName();
    $b  =  checkEmail();
    $c = checkMessage();

    $result = array($a, $b, $c);

    return $result;

Upvotes: 0

Views: 235

Answers (2)

Michael Berkowski
Michael Berkowski

Reputation: 270775

Pass the variables to test into your functions to check them. The way you have it now, it would assume you are using global variables for $name,$message,$email. That would require the use of the global keyword (or some other options) in the functions, but is considered poor practice. Best to pass the variables

Called as:

$a = checkName($name);
$b  =  checkEmail($email);
$c = checkMessage($message);

Definitions

// Pass variable to function
function checkName($name){

    if($name == ''){
        print "Please enter your name!<br />";
        return false;
    }
    else{
        if(strlen($name)<2) {
            print "Your name should be more than 1 characters long!<br />";
            return false;
        }
        else{
            return true;
        }
    }
}
function checkEmail($email){
  // etc...
}
function checkMessage($message){
  // etc...    
}

By the way, as someone who frequently has to maintain old PHP code written by others, I can tell you that it is highly recommended that you do not use variable names like $a,$b,$c. Instead make them readable like $nameResult, $emailResult, $messgeResult.

Upvotes: 3

19greg96
19greg96

Reputation: 2591

In the functions your variables are not defined. If they are defined at all you have to use global $variable in your functions to have them defined in your functions

example: bad:

$var = 'Hello';
function fun () {return $var;}
echo fun () . ' world';

good:

$var = 'Hello';
function fun () {
    global $var;
    return $var;
}
echo fun () . ' world';

Upvotes: 0

Related Questions