Checking for numeric characters in PHP
This tutorial will discuss different ways to check for numeric characters in PHP. We will use regular expressions and built-in functions in PHP.
The functions we will use are ctype_digit
, preg_match
, , strpos
, is_numeric
and filter_var
. These methods are useful because they allow you to exclude some non-numeric characters.
ctype_digit
Checking for numeric characters in PHP
using
ctype_digit
The function accepts a text string and checks whether all string characters are digits. ctype_digit
Exponentiation characters are not considered digits.
example:
<?php
$number_with_exponent = '53e10';
if (ctype_digit('$number_with_exponent')) {
echo $number_with_exponent . ' contains numerical characters';
} else {
echo $number_with_exponent . ' contains non-numeric characters';
}
?>
Output:
53e10 contains non-numeric characters
The function will return false
as the string has exponential characters.
Checking for numeric characters using regular expressions in PHP
A regular expression can check if all characters in a string are digits. The first thing you must do is set up a match pattern that specifically matches digit characters.
In the code block, we preg_match
set up the regular expression matching pattern using the PHP function, and then we pass the string to be checked as the second parameter to preg_match
the function.
example:
<?php
$number_with_exponent = '53e10';
if (preg_match("/^\-?[0-9]*\.?[0-9]+\z/", $number_with_exponent)) {
echo $number_with_exponent . ' contains numerical characters';
} else {
echo $number_with_exponent . ' contains non-numeric characters';
}
?>
Output:
53e10 contains non-numeric characters
Checking for numeric characters excluding exponential sign in PHP
You'll find this approach useful
if you use if
the statement with the function. When you use the function on a string that contains an exponential symbol, the function will return 0 because PHP considers the exponential symbol to be a valid numeric character.is_numeric
is_numeric
true
example:
<?php
$number_with_exponent = '53e10';
$check_string_position = strpos($number_with_exponent, 'e');
$check_is_numeric = is_numeric($number_with_exponent);
if ($check_string_position === false && $check_is_numeric) {
echo $number_with_exponent . ' contains numerical characters';
} else {
echo $number_with_exponent . ' contains non-numeric characters';
}
?>
Output:
53e10 contains non-numeric characters
filter_var
Checking for numeric characters in PHP
using the
The PHP filter_var
function will filter the variable according to the filter. You provide this filter as the second parameter to filter_var
the function.
To check for numeric characters, you will use FILTER_VALIDATE_INT
a filter called .
example:
<?php
$test_numbers = ['53e10', '3.5', '2x3', '200'];
foreach ($test_numbers as $value) {
$check_integer = filter_var($value, FILTER_VALIDATE_INT);
if ($check_integer === false) {
echo $value . ' contains non-numeric characters' . "<br/>";
} else {
echo $value . ' contains numerical characters' . "<br/>";
}
}
?>
Output:
53e10 contains non-numeric characters <br />
3.5 contains non-numeric characters <br />
2x3 contains non-numeric characters <br />
200 contains numerical characters <br />
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Check if a Post exists in PHP
Publish Date:2025/04/13 Views:170 Category:PHP
-
PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss
PHP with Ajax
Publish Date:2025/04/13 Views:139 Category:PHP
-
We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition
Store Div Id in PHP variable and pass it to JavaScript
Publish Date:2025/04/13 Views:51 Category:PHP
-
This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s
Returns the article tag with ID from the action page
Publish Date:2025/04/13 Views:80 Category:PHP
-
Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at
Switching PHP versions on Ubuntu
Publish Date:2025/04/13 Views:78 Category:PHP
-
Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t
Resizing images in PHP
Publish Date:2025/04/13 Views:155 Category:PHP
-
In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using
PHP upload image
Publish Date:2025/04/13 Views:61 Category:PHP
-
We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image
Creating a signature from Hash_hmac() and Sha256 in PHP
Publish Date:2025/04/13 Views:107 Category:PHP
-
PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i
Updating PHP 7.x to 7.4 on CentOS
Publish Date:2025/04/13 Views:131 Category:PHP
-
This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of