mirror of
https://github.com/php/doc-zh.git
synced 2026-03-24 07:02:15 +01:00
147 lines
5.4 KiB
XML
147 lines
5.4 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
||
<!-- $Revision$ -->
|
||
<!-- EN-Revision: e587d0655e426f97b3fcb431453da5030e743b23 Maintainer: Annlix Status: ready -->
|
||
<!-- CREDITS: mowangjuanzi, Luffy -->
|
||
<sect1 xml:id="language.types.numeric-strings" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||
<title>数字字符串</title>
|
||
<para>
|
||
如果一个 PHP <type>string</type> 可以被解释为 <type>int</type> 或 <type>float</type> 类型,则它被视为数字字符串。
|
||
</para>
|
||
|
||
<para>
|
||
PHP 8.0.0 正式可用:
|
||
</para>
|
||
|
||
<informalexample>
|
||
<programlisting>
|
||
<![CDATA[
|
||
WHITESPACES \s*
|
||
LNUM [0-9]+
|
||
DNUM ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*)
|
||
EXPONENT_DNUM (({LNUM} | {DNUM}) [eE][+-]? {LNUM})
|
||
INT_NUM_STRING {WHITESPACES} [+-]? {LNUM} {WHITESPACES}
|
||
FLOAT_NUM_STRING {WHITESPACES} [+-]? ({DNUM} | {EXPONENT_DNUM}) {WHITESPACES}
|
||
NUM_STRING ({INT_NUM_STRING} | {FLOAT_NUM_STRING})
|
||
]]>
|
||
</programlisting>
|
||
</informalexample>
|
||
|
||
<para>
|
||
PHP 也有<emphasis>前导</emphasis>数字字符串的概念。
|
||
这只是一个字符串,其开头类似于数字字符串,后跟任何字符。
|
||
</para>
|
||
|
||
<note>
|
||
<para>
|
||
任何包含字母 <literal>E</literal>
|
||
周围是数字的字符串都将视为以科学计数法表示的数字。这会产生意想不到的效果。
|
||
</para>
|
||
<example>
|
||
<title>科学记数法比较</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
var_dump("0D1" == "000"); // false,“0D1”不是科学计数法
|
||
var_dump("0E1" == "000"); // true,“0E1”是 0 * (10 ^ 1),即 0
|
||
var_dump("2E1" == "020"); // true,“2E1”是 2 * (10 ^ 1),即 20
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
</note>
|
||
|
||
<sect2 xml:id="language.types.numeric-string.conversion">
|
||
<title>在数字上下文中使用的字符串</title>
|
||
<para>
|
||
当一个 <type>string</type> 需要被当作一个数字计算时,(例如:算术运算, <type>int</type>
|
||
类型声明等),则采取以下步骤来确定结果:
|
||
|
||
<orderedlist>
|
||
<listitem>
|
||
<simpara>
|
||
如果 <type>string</type> 是数字,当 <type>string</type>
|
||
是整数字符串并且符合 <type>int</type> 类型的范围限制(即是 PHP_INT_MAX
|
||
定义的值),则解析为 <type>int</type> ,否则解析为 <type>float</type> 。
|
||
</simpara>
|
||
</listitem>
|
||
<listitem>
|
||
<simpara>
|
||
如果上下文允许前导数字和一个 <type>string</type>,如果 <type>string</type> 的前导部分是整数数字字符串且符合 <type>int</type>
|
||
类型限制(由 <constant>PHP_INT_MAX</constant> 定义),则解析为 <type>int</type> ,否则解析为 <type>float</type> 。
|
||
此外,还会导致 <constant>E_WARNING</constant> 级别的错误。
|
||
</simpara>
|
||
</listitem>
|
||
<listitem>
|
||
<simpara>
|
||
如果 <type>string</type> 不是数字,则会抛出一个 <classname>TypeError</classname> 的异常。
|
||
</simpara>
|
||
</listitem>
|
||
</orderedlist>
|
||
</para>
|
||
</sect2>
|
||
|
||
<sect2 xml:id="language.types.numeric-string.prior">
|
||
<title> PHP 8.0.0 之前的行为</title>
|
||
<para>
|
||
在 PHP 8.0.0 之前,
|
||
只有在<emphasis>前导</emphasis>空格的时候,<type>string</type> 才被认为是数字;如果它有<emphasis>尾随</emphasis>空格,则该字符串被视为是前导数字。
|
||
</para>
|
||
|
||
<para>
|
||
在 PHP 8.0.0 之前,当在数字上下文中使用字符串时,它将执行与上述相同的步骤,但有以下区别:
|
||
<itemizedlist>
|
||
<listitem>
|
||
<simpara>
|
||
使用前导数字字符串将导致 <constant>E_NOTICE</constant>
|
||
而不是 <constant>E_WARNING</constant> 错误。
|
||
</simpara>
|
||
</listitem>
|
||
<listitem>
|
||
<simpara>
|
||
如果字符串不是数字,则会导致 <constant>E_WARNING</constant>
|
||
错误并返回 <literal>0</literal> 。
|
||
</simpara>
|
||
</listitem>
|
||
</itemizedlist>
|
||
在 PHP 7.1.0 之前,则既不会导致 <constant>E_NOTICE</constant>,也不会导致 <constant>E_WARNING</constant>。
|
||
</para>
|
||
|
||
<informalexample>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
$foo = 1 + "10.5"; // $foo 是 float (11.5)
|
||
$foo = 1 + "-1.3e3"; // $foo 是 float (-1299)
|
||
$foo = 1 + "bob-1.3e3"; // PHP 8.0.0 起产生 TypeError;在此之前 $foo 是 integer (1)
|
||
$foo = 1 + "bob3"; // PHP 8.0.0 起产生 TypeError;在此之前 $foo 是 integer (1)
|
||
$foo = 1 + "10 Small Pigs"; // PHP 8.0.0 起,$foo 是 integer (11),并且产生 E_WARNING;在此之前产生 E_NOTICE
|
||
$foo = 4 + "10.2 Little Piggies"; // PHP 8.0.0 起,$foo 是 float (14.2),并且产生 E_WARNING;在此之前产生 E_NOTICE
|
||
$foo = "10.0 pigs " + 1; // PHP 8.0.0 起,$foo 是 float (11),并且产生 E_WARNING;在此之前产生 E_NOTICE
|
||
$foo = "10.0 pigs " + 1.0; // PHP 8.0.0 起,$foo 是 float (11),并且产生 E_WARNING;在此之前产生 E_NOTICE
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</informalexample>
|
||
</sect2>
|
||
</sect1>
|
||
<!-- Keep this comment at the end of the file
|
||
Local variables:
|
||
mode: sgml
|
||
sgml-omittag:t
|
||
sgml-shorttag:t
|
||
sgml-minimize-attributes:nil
|
||
sgml-always-quote-attributes:t
|
||
sgml-indent-step:1
|
||
sgml-indent-data:t
|
||
indent-tabs-mode:nil
|
||
sgml-parent-document:nil
|
||
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
||
sgml-exposed-tags:nil
|
||
sgml-local-catalogs:nil
|
||
sgml-local-ecat-files:nil
|
||
End:
|
||
vim600: syn=xml fen fdm=syntax fdl=2 si
|
||
vim: et tw=78 syn=sgml
|
||
vi: ts=1 sw=1
|
||
-->
|