gamefreak13 Posted May 24, 2008 Share Posted May 24, 2008 Is there a cleaner "less code" way of writing this? There are so many IF statements that it's crazy, and there will probably be a few more when I'm done. Would a "case" work? If so, could someone transform this code for me? Otherwise, if isn't any improvement possible then I'm fine with this. // (ALL VARS ARE SET BY $var = $_POST['var']; BUT I REMOVED THEM TO SAVE ROOM) // Input Validations $check = mysql_num_rows(mysql_query("SELECT * FROM members WHERE username='$username'")); if(strlen($username) < 6 | strlen($username) > 16 | !preg_match('/^[a-z\d._-]+$/i', $username) | $check > 0 | strlen($password) < 6 | strlen($password) > 16 | strcmp($password, $cpassword) != 0 | $email == '' | $firstname == '' | $lastname == '' | !preg_match('/^[0-9]+$|^$/', $friendid)) { echo "<p>The following fields are missing. Please go back and correct these fields.</p>\n\n<ul>\n"; if(strlen($username) < 6) { echo "<li>Your username must not be less than 6 characters long</li>\n"; } if(strlen($username) > 16) { echo "<li>Your username must not be more than 16 characters long</li>\n"; } if(!preg_match('/^[a-z\d._-]+$/i', $username)) { echo "<li>Your username contains invalid characters</li>\n"; } if($check > 0) { echo "<li>Your username already in use</li>\n"; } if(strlen($password) < 6) { echo "<li>Your password must not be less than 6 characters long</li>\n"; } if(strlen($password) > 16) { echo "<li>Your password must not be more than 16 characters long</li>\n"; } if(strcmp($password, $cpassword) != 0) { echo "<li>Your passwords do not match</li>\n"; } if($email == '') { echo "<li>Your email is missing</li>\n"; } if($firstname == '') { echo "<li>Your first name missing</li>\n"; } if($lastname == '') { echo "<li>Your last name missing</li>\n"; } // if(!preg_match('/^[0-9]+$/', $friendid)) { if(!preg_match('/^[0-9]+$|^$/', $friendid)) { echo "<li>Your FriendID can only contain numbers</li>\n"; } echo "</ul>\n\n"; } Link to comment https://forums.phpfreaks.com/topic/107129-solved-i-think-i-need-to-setup-a-case/ Share on other sites More sharing options...
Barand Posted May 24, 2008 Share Posted May 24, 2008 In a case statement only one will be executed so it is an alternative to if elseif elseif ... else In your case, several conditions may be true. Link to comment https://forums.phpfreaks.com/topic/107129-solved-i-think-i-need-to-setup-a-case/#findComment-549226 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.