Integers and Floating point numbers in PHP

阅读量:25
2021-04-17

原文: Background

PHP is not a strictly typed language, and many programmers often overlook problems that can be caused with not paying attention to numeric types. The problem is that PHP does not alert you when you have gone outside of the bounds of what an integer can hold. Instead, it silently converts your value to a float (floating point number.)

Why should you care? Mathematic expressions with floats are not always exact ? sometimes they can be way off. PHP, like most programming languages, uses shortcuts to estimate the result of floating point mathematic expressions. The rule of thumb is that the larger the numbers you are working with, the greater amount of estimations involved in finding a result. If you were programming a PHP-based financial application, this could be a huge liability.

The best way to handle large numbers in PHP is to be aware of exactly what type of number you are dealing by knowing the PHP bounds of integers. PHP supplies a constant PHP_INT_MAX which will tell you what the maximum integer is for your instance of PHP. Not all /operating /versions of PHP are the same, so it is good to find the PHP_INT_MAX for each setup. Note that the PHP_INT_MAX also determines the smallest negative number ? just multiply it by -1 to get the PHP integer minimum.

PHP Integer Testing Script

Below is a testing script to help you determine the PHP_INT_MAX:

";   if (is_int($intMax)) {	echo "$intMax is an Int
";}else { echo "$intMax is NOT an Int
";} if (is_int($intMax + 1)) { echo "$intMax+1 is an Int
";}else { echo "$intMax+1 is NOT an Int
";} if (is_int($intMax * (-1))) { echo "$intMax*(-1) is an Int
";}else { echo "$intMax*(-1) is NOT an Int
";}?>

The result of this script:

21474836472147483647 is an Int2147483647+1 is NOT an Int2147483647*(-1) is an Int

PHP Float Testing Script

Now that you know the bounds of integers, what do you do? Truth be told, there is not a whole lot you can do to guarantee the integrity of your floating point numbers. It may be more important to know which figures are the result of math involving floating point numbers, so you can disclose this to your audience, and act accordingly. See the simple example below that shows how poorly PHP can do math involving floating point numbers:

";}else {	echo "Test 1: Are NOT Equal
";} //TEST 2$number = 216897510871;$original_number = $number; $number *= 11123.75;$number /= 11123.75; if ($original_number == $number) { echo "Test 2: Are Equal
";}else { echo "Test 2: Are NOT Equal
";}?>

The result of this script:

Test 1: Are EqualTest 2: Are NOT Equal

声明:本文原创发布星网无限,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@处理

THE END

发表评论

相关推荐

  • 有关fgets()函数的文章推荐10篇

    以下正文:这篇文章主要介绍了PHP文件读写操作相关函数总结,本文总结了fwrite()、fread()、fgets()、fgetc()、file()、readf ...

    阅读量:68
    2021-04-19
  • php递归与无限分类实例详解

    这篇文章主要介绍了php实现递归与无限分类的方法,涉及php的递归操作技巧,需要的朋友可以参考下本文实例讲述了php实现递归与 ...

    阅读量:47
    2021-04-19
  • CI框架中zip类的使用

    CI框架自带的zip类简单实用,本文就来简单说一下ci框架的zip类的使用,需要的朋友可以参考下CI框架自带的zip类简单实用,本文 ...

    阅读量:46
    2021-04-18
  • SWFUpload插件上传文件的代码

    这篇文章主要介绍了文件上传之SWFUpload插件(代码),实现此代码主要分为两部分:1.前台文件和 2.后台文件upload.php,需要 ...

    阅读量:46
    2021-04-18
  • 关于destoon的URL Rewrite设置方法

    这篇文章主要介绍了destoon的URL Rewrite(伪静态)设置方法,需要的朋友可以参考下1、如果您的服务器支持.htaccess,则无需设 ...

    阅读量:50
    2021-04-18