Home » How to Validate Phone Number Using PHP?

How to Validate Phone Number Using PHP?

  • by
validate phone number using PHP

Last Updated on June 22, 2021 by admin

In this article, we will learn about validate phone number using PHP

The debate about a programming language being good or bad is never-ending since the verdict depends upon countless factors.

One such controversial language is PHP.

PHP, which is an acronym for Hypertext Preprocessor, is an open-source scripting language that is used widely around the globe.

Furthermore, PHP is free to use and download and all scripts are executed on the server.

Many other factors make PHP a popular language.

For example, it allows users to create page dynamic page content, create forms, sends/receive cookies, encrypts data, controls user access, etc.

What makes it stand out from HTML is that using PHP, you can give outputs that include PDF files, images, flash movies, etc

Other than that, it also allows you to output texts such as XHTML and XML.

There might be some beginners who are wondering why they should learn PHP.

  • To answer that question, look at the following points:
  • PHP is compatible with almost all servers
  • Supports a wide range of database
  • Easy to learn
  • Runs efficiently on the server-side
  • The latest version (PHP 7) has improved error handling, strict type declarations for function arguments, and new operators such as the spaceship operator.

Validate Phone Number Using PHP

Now that we have learned what makes PHP unique, let us talk about a common issue that our programmers face today.

The problem that I’m talking about is “how to validate a phone number using PHP?”.

There are many instances where a web developer will need to validate a phone number that he receives through a form.

This article will discuss how you can easily solve this problem so without further ado, let us get straight into it.

function validate_phone_number($phone)
// Allow +, - and . in phone number
$filtered_phone_number = filter_var($phone, FILTER_SANITIZE_NUMBER_INT);
// Remove "-" from number
$phone_to_check = str_replace("-", "", $filtered_phone_number);

// Check the lenght of number
// This can be customized if you want phone number from a specific country
if (strlen($phone_to_check) < 10 || strlen($phone_to_check) > 14) {
return false;
} else {
return true;


A clever and efficient trick that a developer may use is to allow the user to enter his phone number using the country code and in some cases, using the “+” sign before the country code.

To do this, we will use a PHP function.

In the very first step, the function has made to take the phone number as a parameter.

Furthermore, to make this program even more efficient, by using the FILTER_SANITIZE_NUMBER_INT filter.

We have restricted to use of special characters so that the input is given in numbers, “+”, “-” and “.” signs only.

The reason why “+” and “-” signs are allowed is that sometimes phone numbers can be written in the form of +92-xxxx-xxxxxxx.
Now, we tell our code to remove the “-” sign from the phone number that the user has entered and to do this, we make use of a special PHP function called “str_replace”.

Next, our program checks whether the entered phone number is of the right length, and based on that, it returns a value of either True or False.

The length of a phone number is usually 10 to 14 characters long depending upon the country code.
The above code will help you validate a phone number properly using PHP.

Read more: How to Convert a Python String to Integers?


Leave a Reply

Your email address will not be published. Required fields are marked *