0

I have a form, and I want to validate it with PHP, for that I going to make just one if the problem is I want to detect where is the variable that equal to 0

My code

if(isset($_POST["submit"])){
 $name = $_POST["name"];
 $lname = $_POST["lname"];

 if($name == "" || $lname == ""){
  echo "Please, Enter All informations";
 }
 else {
    $namepr = preg_match("/^[A-Za-z]{3,}$/",$name);
    $lnamepr = preg_match("/^[A-Za-z]{3,}$/",$lname);
  if($namepr == 0 || $lnamepr == 0){
   <!-- here I want to select the variable that equal to 0 -->
  }
  else {
   echo "Your name is : ".$name."<br>Your Last name is : ".$lname 
  }
 }
}

my question is in the comment part, I want to show the variable equal to 0, I have two variable, I want to select the variable equal to 0

saadsaad
  • 35
  • 12
  • So what is your question? Do you have a specific part you need help with? – Danny Wilson Nov 26 '16 at 12:52
  • @DannyWilson my question is in the comment part, I want to show the variable equal to 0, I have two variable, I want to select the variable equal to 0 – saadsaad Nov 26 '16 at 12:54

1 Answers1

1

For your issue, you can use a ternary operator:

if($namepr == 0 || $lnamepr == 0){
    $myVar = ($namepr == 0 ? $namepr : $lnamepr);
    // do something with $myVar
}

or of course, a standard if block:

if($namepr == 0 || $lnamepr == 0){
    if($namepr == 0){
        $myVar = $namepr;
    } else {
        $myVar = $lnamepr;
    }
    // do something with $myVar
}  
Panda
  • 6,955
  • 6
  • 40
  • 55
SierraOscar
  • 17,507
  • 6
  • 40
  • 68