I wanted to allow only specific email domain. Actually I did it. What i wanted to ask why my first code did not work at all.
I am just trying to learn PHP so that the question may seem silly, sorry for that.
Here is my code:
function check_email_address($email) {
$checkmail = print_r (explode("@",$email));
$container = $checkmail[1];
if(strcmp($container, "gmail.com")) {
return true;
}else {
return false;
}
}
Check out the documentation for
strcmp(), it will return 0 of the two strings are the same, so that’s the check you want to be doing. Also, you’re usingprint_r()when you shouldn’t be, as mentioned by the other answerers.Anyway, here’s how I would have done the function – it’s much simpler and uses only one line of code:
It uses the
strstr()function and thestrtolower()function to get the domain name and change it to lower case, and then it checks if it isgmail.comor not. It then returns the result of that comparison.