Home >Backend Development >PHP Problem >How to determine whether there is a decimal point in php

How to determine whether there is a decimal point in php

青灯夜游
青灯夜游Original
2022-04-20 20:12:324128browse

php method to determine whether there is a decimal point: 1. Use the "strpos(numeric string,'.')" syntax. If the position where the decimal point first appears in the string is returned, there is a decimal point; 2 , use the "strrpos(numeric string,'.')" statement, if the position of the last occurrence of the decimal point in the string is returned, then there is.

How to determine whether there is a decimal point in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php to determine whether there is Decimal point method

Method 1: Use the strpos() function

Use the strpos() function to get the first occurrence of the decimal point in the string The position, syntax:

strpos(数字字符串,'.')

If the position is returned, it is present, if FALSE is returned, there is no.

Example:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$nubs = 34123.456;
if (strpos($nubs, &#39;.&#39;) !== false) {
echo &#39;是小数,有小数点&#39;;
} else {
echo &#39;不是小数,没有小数点&#39;;
}
?>

How to determine whether there is a decimal point in php

Method 2: Use strrpos() function

Use strrpos( ) function gets the position of the last occurrence of the decimal point in the string, syntax:

strrpos(数字字符串,&#39;.&#39;)

If the position is returned, it is present, if it returns FALSE, it is not.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$nubs = 34123;
if (strrpos($nubs, &#39;.&#39;) !== false) {
echo &#39;是小数,有小数点&#39;;
} else {
echo &#39;不是小数,没有小数点&#39;;
}
?>

How to determine whether there is a decimal point in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to determine whether there is a decimal point 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