What is the purpose of the function checkPhone in the PHP code provided?
The purpose of the function checkPhone in the PHP code provided is to validate a phone number input by the user. However, the current implementation of the function is incorrect as it is missing the regular expression pattern to validate the phone number format. To fix this issue, we need to update the regular expression pattern used in the function to properly validate phone numbers.
function checkPhone($phone) {
// Regular expression pattern to validate phone number format
$pattern = "/^\d{10}$/";
// Check if the phone number matches the pattern
if (preg_match($pattern, $phone)) {
return true;
} else {
return false;
}
}
// Example usage
$phone = "1234567890";
if (checkPhone($phone)) {
echo "Phone number is valid.";
} else {
echo "Invalid phone number.";
}
Keywords
Related Questions
- What are some potential pitfalls when dealing with parsing CSV data containing variables in a specific format like "wert1=text, wert2=text, wert3=number" in PHP?
- What are the implications of moving the first entry of a select box outside of the box itself, such as placing it above as a label?
- What are the potential issues with passing Session IDs in URLs in PHP?