How to use ctype_digit Function In PHP
PHP provides a built-in function called ctype_digit() that can be used to check if all characters in a string are numeric digits.
Example: This example illustrates checking if string contains only numeric digits using ctype_digit Function in PHP.
<?php
function isNumeric($str)
{
return ctype_digit($str);
}
// Driver code
$str1 = "12345";
$str2 = "gfg123";
echo isNumeric($str1) ? "Numeric String" : "Non Numeric String";
echo "\n";
echo isNumeric($str2) ? "Numeric String" : "Non Numeric String";
?>
Output
Numeric String Non Numeric String
How to check if string contains only numeric digits in PHP ?
Given a string, the task is to check whether the string contains only numeric digits in PHP. It is a common task to verify whether a given string consists of only numeric digits in PHP. It can be useful when dealing with user input validation, form submissions, or data processing tasks where numeric values are expected.
For instance, the below examples illustrate the concept:
Input: str = "1234"
Output: true
Input: str = "w3wiki2020"
Output: false
Here, we will cover three common scenarios for checking if the string contains only numeric digits.
Table of Content
- Using ctype_digit Function
- Using Regular Expressions
- Using is_numeric Function
- Using filter_var Function
Contact Us