saadsaad saadsaad - 7 days ago 6
PHP Question

Show where is equal to 0 (PHP)

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

Answer

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
}  
Comments