0

If the function validateCustomForm() returns nothing, then that means all of the variables are set. I tried using empty() but I figured since there is something in the function that it will always return false.

How can I check if a function returns something?

Script in action here

<?php

function validateCustomForm(){

$os0 = "";
$os1 = "5";
$os2 = "6";
$os3 = "5";
    if(!empty($os0)){
    //do nothing
    }else{
            $w = "width is missing";
    echo $w;
    }
    if(!empty($os1)){
    //do nothing
    }else{
            $h = "height is missing";
    echo $h;
    }
    if(!empty($os2)){
    //do nothing
    }else{
            $c = "color is missing";
    echo $c;
    }
    if(!empty($os3)){
    //do nothing
    }else{
            $q = "qty is missing";
    echo $q;
    }

    }//end function

$valid = validateCustomForm();

if(!empty($valid)){
echo "something is missing";
} else{
echo "all good";
}


?>
7
  • 1
    remove the echo s, just return a value to be echoed or empty otherwise, or false if you prefer Commented Sep 4, 2014 at 4:21
  • 1
    I see no return statements so $valid will always be void (null) Commented Sep 4, 2014 at 4:21
  • when you talk about return there has to be a return statement in your function Commented Sep 4, 2014 at 4:25
  • 1
    quick fix: codepad.org/fjUd1oRC Commented Sep 4, 2014 at 4:28
  • @Dagon I love it. Wish you would have answered so I could accept your answer. Commented Sep 4, 2014 at 8:45

2 Answers 2

2
<?php

function validateCustomForm(){

    $os0 = "";
    $os1 = "5";
    $os2 = "6";
    $os3 = "5";

    $errors = array();

    if(empty($os0)){
        $errors[] = "width is missing";
    }
    if(empty($os1)){
        $errors[] = "height is missing";
    }
    if(empty($os2)){
        $errors[] = "color is missing";
    }
    if(empty($os3)){
        $errors[] = "qty is missing";
    }
    if(!empty($errors)) {
        return $errors;
    }
    return TRUE;
}//end function

$valid = validateCustomForm();

if($valid !== TRUE){
    echo "something is missing: ";
    echo implode(',' , $valid);
} else{
   echo "all good";
}


?>
Sign up to request clarification or add additional context in comments.

Comments

0

You should return something in function instead of echo. Like below

<?php

function validateCustomForm(){

$os0 = "";
$os1 = "5";
$os2 = "6";
$os3 = "5";
$error = false;
    if(!empty($os0)){
    //do nothing
    }else{
            $w = "width is missing";
    $error = true;
    }
    if(!empty($os1)){
    //do nothing
    }else{
            $h = "height is missing";
     $error = true;
    }
    if(!empty($os2)){
    //do nothing
    }else{
            $c = "color is missing";
     $error = true;
    }
    if(!empty($os3)){
    //do nothing
    }else{
            $q = "qty is missing";
     $error = true;
    }
    return $error;
    }//end function

$valid = validateCustomForm();

if(!empty($valid)){
echo "something is missing";
} else{
echo "all good";
}


?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.