dk4210 Posted April 18, 2011 Share Posted April 18, 2011 Hello Guys, I have a question about doing an IF statement in a function. Here is my code function checkEmail($email){ return preg_match('/^\S+@[\w\d.-]{2,}\.[\w]{2,6}$/iU', $email) ? TRUE : FALSE; } How would I do the IF statement to test if the email is True or False? Is it like this? function checkEmail($email){ return preg_match('/^\S+@[\w\d.-]{2,}\.[\w]{2,6}$/iU', $email) ? TRUE : FALSE; if ($email == "False") { Do something here } } Thanks for you help! Link to comment https://forums.phpfreaks.com/topic/234069-function-if-statement-question/ Share on other sites More sharing options...
kenrbnsn Posted April 18, 2011 Share Posted April 18, 2011 No, the return statement in the function ends the execution of the function. You check the validity of the email address by calling the function: <?php function checkEmail($email){ return preg_match('/^\S+@[\w\d.-]{2,}\.[\w]{2,6}$/iU', $email) ? TRUE : FALSE; } $email_to_check = '[email protected]'; if (!checkEmail($email_to_check)) { echo 'the email address is not correct'; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/234069-function-if-statement-question/#findComment-1203050 Share on other sites More sharing options...
dk4210 Posted April 18, 2011 Author Share Posted April 18, 2011 Thanks for the quick response Ken. What if I wanted to check to just make sure that it's a valid email address in the function. Not necessarily a specific email address? For example here is one I done for the price function check_Price($price,$member_id,$description,$ip){ if (!is_numeric($price)) { $t_error="6"; $member_id = $member_id; notify_Admin($t_error,$member_id,$ip); logOut ($t_error); exit; } Link to comment https://forums.phpfreaks.com/topic/234069-function-if-statement-question/#findComment-1203060 Share on other sites More sharing options...
kenrbnsn Posted April 18, 2011 Share Posted April 18, 2011 The preg_match function in your function is checking to see whether the email is valid and is returning "true" or "false" accordingly. Ken Link to comment https://forums.phpfreaks.com/topic/234069-function-if-statement-question/#findComment-1203070 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.