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)".
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)
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(数字字符串, '.')
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, '.') !== false) { echo '这个数是小数'; } else { echo '这个数不是小数'; } ?>
Output result:
<?php $nubs = 34123; if (strpos($nubs, '.') !== false) { echo '这个数是小数'; } else { echo '这个数不是小数'; } ?>
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!