Use php to verify whether a string is a number

autoload
Release: 2023-03-08 22:32:01
Original
2540 people have browsed it

Use php to verify whether a string is a number

It is very easy to use regular expressions to verify whether a string is a number in php. The most important thing is how to write regular expressions and master regular expressions. As for how to write expressions, here we use regular expressions to list ways to judge numbers.

The first method:

Use the function is_numeric() to verify

bool is_numeric ( mixed $var )
Copy after login
  • $var: Variable to be detected

  • Return value: TRUE if the specified variable is a number or numeric string, otherwise it returns FALSE, pay attention to floating point Type returns a null value, which is FALSE.

if(is_numeric($str)) echo('是数字'); else echo('不是数字');
Copy after login

Second method:

Use regular expression verification

Copy after login

Recommended: "php video tutorial" "php tutorial"

The above is the detailed content of Use php to verify whether a string is a number. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!