Home  >  Article  >  Backend Development  >  How to determine whether a positive number is positive in php

How to determine whether a positive number is positive in php

藏色散人
藏色散人Original
2021-11-15 09:18:193614browse

How to judge whether a positive number is positive in php: 1. Judgment through regular expressions; 2. Judgment combined with floor function; 3. Judgment through functions such as is_numeric.

How to determine whether a positive number is positive in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

How does PHP determine whether it is a positive number?

php determines whether a variable is a positive integer

Method one:

if(preg_match("/^[1-9][0-9]*$/" ,$amount)){
 die('是正整数');
}

Method two:

$ num = '45 .7' ;
if((floor($ num)- $ num)!== 0){
  exit(“ 不是正整数!”);
} else {
  exit(“ 是正整数!”);
}

Method three:

$ num = '12' ;
if(floor($ num)== $ num){
  exit(“ 是正整数!”);
} else {
  exit(“ 不是正整数!”);
}

Method 4:

if(! is_numeric($ jp_total)|| strpos($ jp_total ,“。”)!== false){
  die(“ 不是整数”);
} else {
  die(“ 是整数”);
}

Recommended learning: "PHP Video Tutorial"

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

Statement:
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