How to determine whether a variable is an integer in php

L
Release: 2023-03-01 11:10:01
Original
3959 people have browsed it

How to determine whether a variable is an integer in php

Two methods for php to determine whether a variable is an integer

1. If you are sure that your variable is a number , PHP has a built-in judgment function: is_int detects whether the variable is an integer;

2. But sometimes the variables we receive are numbers or numerical strings (such as form input, they are usually strings), In this way, we cannot use the daois_int() function to simply determine whether it is an integer. We need to use the is_numeric() and strpos() functions to determine whether it is an integer:

(1) Use the is_numeric() function to determine whether it is an integer. It is a number or a string of numbers;

(2) Use strpos() to determine whether the number contains a decimal point;

if(!属is_numeric($age)||strpos($age,".")!==false){
  echo "不是整数";
}else{
  echo "是整数";
}
Copy after login

Recommended tutorial: "PHP Tutorial"

The above is the detailed content of How to determine whether a variable is an integer in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template