Home > Backend Development > PHP Problem > How to determine whether a number can be a decimal in php

How to determine whether a number can be a decimal in php

WBOY
Release: 2023-03-13 21:18:01
Original
2791 people have browsed it

In PHP, you can use the strpos() function to determine whether a number can be a decimal. This function can determine whether the decimal point exists in the number. If it does not exist, the number is not a decimal and will return false. Otherwise, If it exists, it is a decimal; the syntax is "if(strpos(numeric string,'.') !== false)".

How to determine whether a number can be a decimal in php

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

php determines a number Can it be a decimal?

#1. In PHP, you can use the strpos() function to find the first occurrence of a string in another string. The syntax of the strpos() function is:

strpos(string,find,start)
Copy after login

It should be noted that:

The parameter strings is required, indicating that the string to be searched is specified; the find parameter is also required, used to specify The string to be found; the start parameter is optional and specifies where to start the search.

The returned result is the position of the first occurrence of the string in another string. If the string is not found, FALSE is returned.

To determine whether it is a decimal, we can use the strpos function to find whether the decimal point appears in the number string. The code is as follows:

strpos(数字字符串, '.')
Copy after login

If it is a decimal, the returned result is the first decimal point If the position that appears this time is not a decimal, there will be no decimal point, and the returned result will be false.

2. You can judge whether the result returned by the strpos function in the previous step is equal to flase. If it is true, it means it is a decimal. If it is false, it means it is not a decimal.

The code is as follows:

<?php
$nubs = 34123.456;
if (strpos($nubs, &#39;.&#39;) !== false) {
echo &#39;这个数是小数&#39;;
} else {
echo &#39;这个数不是小数&#39;;
}
?>
Copy after login

Output result:

How to determine whether a number can be a decimal in php

##Example:

<?php
$nubs = 34123;
if (strpos($nubs, &#39;.&#39;) !== false) {
echo &#39;这个数是小数&#39;;
} else {
echo &#39;这个数不是小数&#39;;
}
?>
Copy after login
Output Result:


How to determine whether a number can be a decimal in php

If you are interested, you can click on "

PHP Video Tutorial" to learn more about PHP knowledge.

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