Check if String Contains Digits Only in PHP


Check if String Contains Digits Only in PHP

Demo Download

In this tutorial, I will show you how to check if string contains digits only in PHP. There are various occasions when you need to check whether string or variable contains only numeric digits using PHP.

PHP provides a built in is_numeric() function to check if string is number or not. This function returns true if the value is number. However, the problem with this function is that it returns true even if string contains a float number.

And if you purely want to check if string is digits only without decimal like float number then you have to use other ways to get your desired result.

There are many ways to do that I will share two methods with you to check if string contains numeric digits using PHP.

  1. Check String Using preg_match() Function
  2. Check String Using Custom isNumber() Function

Method 01: Check String Using preg_match() Function

The first method is that using a PHP preg_match() function, this function perform the regular expression and returns 1 if it found the match.

I will use this function and pass it the two arguments, first argument will be the pattern which will check that string is only numeric digits and second argument will be our variable which contains our string to check.

Here is the code, just create an index.php file and paste the following code in it.

<h2>Check String Using preg_match() Function</h2>
<?php
$valid_string = '11234';
$invalid_string = '1123.4';
echo "<p>Valid String: $valid_string</p>";
echo "<p>Invalid String: $invalid_string</p>";

// Check String Using preg_match() Function
$pattern = '/^[0-9]+$/';

// Valid Numeric String Check
if ( preg_match($pattern, $valid_string) ) {
    echo '<p class="success">String contains only numeric digits</p>';
} else {
    echo '<p class="error">String does not contain only numeric digits</p>';
}

// Invalid Numeric String Check
if ( preg_match($pattern, $invalid_string) ) {
    echo '<p class="success">String contains only numeric digits</p>';
  } else {
    echo '<p class="error">String does not contain only numeric digits</p>';
  }
?>

In the above code, I have set two variables, one contains a valid digits only string while other contains an invalid digits string as it also contains decimal. If string contain alphabets, it will also consider as an invalid string.

After that I have created our pattern variable which is passed to our preg_match() function, if our variable string passed the pattern then it is a valid digits string.

Method 02: Check String Using Custom isNumber() Function

The second method is to check string using our custom isNumber() function, first I have to create this function then I will pass our string variable to check if it contains a valid numeric digits only.

Create another is_number.php file and paste the below code in it.

<h2>Check String Using Custom isNumber() Function</h2>
<?php
$valid_string = '11234';
$invalid_string = '1123.4';
echo "<p>Valid String: $valid_string</p>";
echo "<p>Invalid String: $invalid_string</p>";

//Check String Using Custom isNumber() Function
// Function to Check Numeric Digits Only
function isNumber($s) 
{ 
    for ($i = 0; $i < strlen($s); $i++) 
        if (is_numeric($s[$i]) == false) 
            return false; 
    return true; 
} 

// Valid Numeric String Check
if( (isNumber($valid_string)==1) ){
    echo '<p class="success">String contains only numeric digits</p>';
}else{
    echo '<p class="error">String does not contain only numeric digits</p>';
    }

// Invalid Numeric String Check
if( (isNumber($invalid_string)==1) ){
    echo '<p class="success">String contains only numeric digits</p>';
}else{
    echo '<p class="error">String does not contain only numeric digits</p>';
    }    
?>

Our custom isNumber() function also return 1 if the string is a valid digits only. So I will simply pass our string variable to isNumber() function and check if it return 1 or not.

If you are taking input from your user then you can also place client side validation to get the data in your desired data type, I have also shared a tutorial about how to insert only numbers using jQuery.

Conclusion

I hope by now you know how to check if string contains digits only using PHP. When you are working with PHP application development, you may need to check string data type before performing any operations or adding that variable value into our database.

This help us to validate our data before further processing.

If you found this tutorial helpful, share it with your friends and developers group.

I spent several hours to create this tutorial, if you want to say thanks so like my page on Facebook, Twitter and share it.

Demo Download

Facebook Official Page: All PHP Tricks

Twitter Official Page: All PHP Tricks

Article By
Javed Ur Rehman is a passionate blogger and web developer, he loves to share web development tutorials and blogging tips. He usually writes about HTML, CSS, JavaScript, Jquery, Ajax, PHP and MySQL.
  1. Checking if ($s==intval($s)) works and is pretty compact.

    I don’t know about its performance though.

Leave a Reply

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