<tfoot id='Dcd3G'></tfoot>

      <small id='Dcd3G'></small><noframes id='Dcd3G'>

      <i id='Dcd3G'><tr id='Dcd3G'><dt id='Dcd3G'><q id='Dcd3G'><span id='Dcd3G'><b id='Dcd3G'><form id='Dcd3G'><ins id='Dcd3G'></ins><ul id='Dcd3G'></ul><sub id='Dcd3G'></sub></form><legend id='Dcd3G'></legend><bdo id='Dcd3G'><pre id='Dcd3G'><center id='Dcd3G'></center></pre></bdo></b><th id='Dcd3G'></th></span></q></dt></tr></i><div id='Dcd3G'><tfoot id='Dcd3G'></tfoot><dl id='Dcd3G'><fieldset id='Dcd3G'></fieldset></dl></div>
        <bdo id='Dcd3G'></bdo><ul id='Dcd3G'></ul>
        <legend id='Dcd3G'><style id='Dcd3G'><dir id='Dcd3G'><q id='Dcd3G'></q></dir></style></legend>

        为什么 PHP 认为 0 等于字符串?

        Why does PHP consider 0 to be equal to a string?(为什么 PHP 认为 0 等于字符串?)
        <i id='P5RsC'><tr id='P5RsC'><dt id='P5RsC'><q id='P5RsC'><span id='P5RsC'><b id='P5RsC'><form id='P5RsC'><ins id='P5RsC'></ins><ul id='P5RsC'></ul><sub id='P5RsC'></sub></form><legend id='P5RsC'></legend><bdo id='P5RsC'><pre id='P5RsC'><center id='P5RsC'></center></pre></bdo></b><th id='P5RsC'></th></span></q></dt></tr></i><div id='P5RsC'><tfoot id='P5RsC'></tfoot><dl id='P5RsC'><fieldset id='P5RsC'></fieldset></dl></div>

        <tfoot id='P5RsC'></tfoot>
        • <small id='P5RsC'></small><noframes id='P5RsC'>

        • <legend id='P5RsC'><style id='P5RsC'><dir id='P5RsC'><q id='P5RsC'></q></dir></style></legend>

            • <bdo id='P5RsC'></bdo><ul id='P5RsC'></ul>
                <tbody id='P5RsC'></tbody>

                • 本文介绍了为什么 PHP 认为 0 等于字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下代码:

                  $item['price'] = 0;
                  /* Code to get item information goes in here */
                  if($item['price'] == 'e') {
                      $item['price'] = -1;
                  }
                  

                  它旨在将项目价格初始化为0,然后获取有关它的信息.如果价格被告知为e",则表示交换而不是卖出,它作为负数存储在数据库中.

                  It is intended to initialize the item price to 0 and then get information about it. If the price is informed as 'e' it means an exchange instead of a sell, which is stored in a database as a negative number.

                  也有可能将价格保留为 0,因为该项目是奖金或因为价格将在稍后设置.

                  There is also the possibility to leave the price as 0, either because the item is a bonus or because the price will be set in a later moment.

                  但是,当价格没有设置时,它的初始值为 0,上面指示的 if 循环评估为真,价格设置为 -1.也就是说,它认为 0 等于 'e'.

                  But, whenever the price is not set, which leaves it with the initial value of 0, the if loop indicated above evaluates as true and the price is set to -1. That is, it considers 0 as equal to 'e'.

                  如何解释?

                  当价格为 0(初始化后)时,行为不稳定:有时 if 评估为 true,有时评估为 false.*

                  When the price is provided as 0 (after initialization), the behavior is erratic: sometimes the if evaluates as true, sometimes it evaluates as false.*

                  推荐答案

                  你正在做 == 为你整理类型.

                  You are doing == which sorts out the types for you.

                  0 是一个 int,所以在这种情况下,它会将 'e' 转换为一个 int.不能作为一个解析,将成为 0.字符串 '0e' 将变为 0 并匹配!

                  0 is an int, so in this case it is going to cast 'e' to an int. Which is not parsable as one and will become 0. A string '0e' would become 0 and would match!

                  使用 ===

                  来自 PHP.net:

                  使用 == 和其他非严格的字符串和数字之间的比较比较运算符目前通过将字符串转换为数字来工作,并随后对整数或浮点数进行比较.这导致许多令人惊讶的比较结果,最值得注意的是那就是 0 == foobar";返回真.

                  Comparisons between strings and numbers using == and other non-strict comparison operators currently work by casting the string to a number, and subsequently performing a comparison on integers or floats. This results in many surprising comparison results, the most notable of which is that 0 == "foobar" returns true.

                  然而,这种行为在 PHP 8.0:

                  当与数字字符串进行比较时,PHP 8 使用数字比较.否则,它将数字转换为字符串并使用字符串比较.

                  When comparing to a numeric string, PHP 8 uses a number comparison. Otherwise, it converts the number to a string and uses a string comparison.

                  PHP 7

                  0 == 'foobar' // true
                  0 == '' // true
                  4 == '4e' // true (4e is cast as a number and becomes 4)
                  

                  PHP 8 在进行比较之前将数字转换为字符串

                  PHP 8 converts numbers to strings before making comparisons

                  0 == 'foobar' // false
                  0 == '' // false
                  4 == '4e' // false ('4e' is considered non-numeric therefore 4 is cast as a string and becomes '4')
                  

                  这是一个重大变化,因此它是在一个新的主要 PHP 版本中实现的.此更改破坏了依赖于旧行为的脚本的向后兼容性.

                  This is a major change therefore it was implemented in a new major PHP version. This change breaks backward compatibility in scripts that depend on the old behavior.

                  这篇关于为什么 PHP 认为 0 等于字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Converting between timezones in PHP(在 PHP 中的时区之间转换)
                  PHP - strtotime, specify timezone(PHP - strtotime,指定时区)
                  Get current date, given a timezone in PHP?(获取当前日期,给定 PHP 中的时区?)
                  List of US Time Zones for PHP to use?(PHP 使用的美国时区列表?)
                  How to detect Ambiguous and Invalid DateTime in PHP?(如何在 PHP 中检测不明确和无效的 DateTime?)
                  How to update timezonedb in PHP (updating timezones info)?(如何在 PHP 中更新 timezonedb(更新时区信息)?)

                        <tbody id='NPn3G'></tbody>

                      <small id='NPn3G'></small><noframes id='NPn3G'>

                      <tfoot id='NPn3G'></tfoot>

                      1. <legend id='NPn3G'><style id='NPn3G'><dir id='NPn3G'><q id='NPn3G'></q></dir></style></legend>

                          <i id='NPn3G'><tr id='NPn3G'><dt id='NPn3G'><q id='NPn3G'><span id='NPn3G'><b id='NPn3G'><form id='NPn3G'><ins id='NPn3G'></ins><ul id='NPn3G'></ul><sub id='NPn3G'></sub></form><legend id='NPn3G'></legend><bdo id='NPn3G'><pre id='NPn3G'><center id='NPn3G'></center></pre></bdo></b><th id='NPn3G'></th></span></q></dt></tr></i><div id='NPn3G'><tfoot id='NPn3G'></tfoot><dl id='NPn3G'><fieldset id='NPn3G'></fieldset></dl></div>
                          • <bdo id='NPn3G'></bdo><ul id='NPn3G'></ul>