function is_alpha($str) { return preg_match('/^[a-zA-Z]+$/', $str); } // usage if (is_alpha('HelloWorld')) { echo 'String contains only alphabetical characters'; } else { echo 'String contains non-alphabetical characters'; }
function is_valid_email($email) { return filter_var($email, FILTER_VALIDATE_EMAIL); } // usage if (is_valid_email('example@example.com')) { echo 'Valid email address'; } else { echo 'Invalid email address'; }This example uses the filter_var() function with the FILTER_VALIDATE_EMAIL constant, which verifies that the given string is a valid email address. The function returns the email address if it is valid, or boolean false if it is not valid. The PHP self-validation examples can be classified under the PHP Core packages/library.