mercredi 26 septembre 2018

Transform an if.. elseif chain of distinct conditions into case switch

Simple curiosity, is there any way to convert what this following into a switch loop?

PHP :

        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                  $error = array('type' => 'error', 'value' => 'email');
             }
        elseif (!preg_match($regex_name, $username)) {
                  $error = array('type' => 'error', 'value' => 'username');
             }
        elseif (!preg_match($regex_name, $firstname) && preg_match($regex_name, $lastname)) {
                  $error = array('type' => 'error', 'value' => 'name');
             }
        elseif ($password !== $password_conf) {
                  $error = array('type' => 'error', 'value' => 'password');
             }
        elseif (checkdate($birthday_d, $birthday_m, $birthday_y) == false) {
            $error = array('type' => 'error', 'value' => 'date');
        }
        else {
            $error = array('type' => 'success');
        }

Thanks.

Aucun commentaire:

Enregistrer un commentaire