Preg_match check for Yes or No

Rocky1948

New member
I am trying to check whether the answer to the option YES or No on a form. I tried this but it doesn't work:
PHP:
if preg_match('~^(?:Yes|no)$~i', $rewd]){
		$rewd = mysqli_real_escape_string ($dbc, $trimmed['rewd']);
	} else{
		echo '<p class="error">Please answer Yes or No!</p>';
	}
Here is the error:
Parse error: syntax error, unexpected 'preg_match' (T_STRING), expecting '(' in C:\wamp64\www\Test\Form_dat.php on line 69
I can't see any mis match with the brackets, so what is causing this error?
Can anyone explain why I'm getting an error or suggest a alternative way of checking for if the answer has been submitted?
 
first of all, you need to add brackets for the if statement:
PHP:
if ( /* condition */ ) {
}
second thing, do not use regex if you really don't need it:
Code:
$answer = strtolower($rewd);
switch ($answer) {
    case 'yes':
        // some code
        break;
    case 'no':
        // some code
        break;
    default:
        // some code to handle situation, when answer is invalid
        break;
}
 
Back
Top