如何在 PHP 中将字符串转换为 int?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6967375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:42:06  来源:igfitidea点击:

How to cast string to int in PHP?

phptype-conversion

提问by RiaD

My code

我的代码

<?php
var_dump('0xD' * 1 );
var_dump((int)'0xD');
var_dump(intval('0xD'));
var_dump((float)'0xD');

Actual result:

实际结果:

int(13)
int(0)
int(0)
float(0)

Why result of first two conditions are not same? Can you provide me correct documentation?

为什么前两个条件的结果不一样?你能给我提供正确的文件吗?

All that I found is

我发现的只是

  • Type Casting: Type casting in PHP works much as it does in C: the name of the desired type is written in parentheses before the variable which is to be cast. It may not be obvious exactly what will happen when casting between certain types. For more information, see these sections:...
  • Converting to integer: From strings: See String conversion to numbers
  • String conversion to numbersWhen a string is evaluated in a numeric context, the resulting value and type are determined as follows.
  • 类型转换:PHP 中的类型转换与在 C 中的工作方式非常相似:所需类型的名称写在要转换的变量之前的括号中。在某些类型之间进行转换时会发生什么可能并不明显。有关更多信息,请参阅以下部分:...
  • 转换为整数:从字符串:请参阅字符串转换为数字
  • 字符串到数字的转换在数字上下文中计算字符串时,结果值和类型按如下方式确定。

So, As far as I understand, docs say that casting to int directly and by putting in numeric context should be same. What is my mistake?

所以,据我所知,文档说直接转换为 int 和通过放入数字上下文应该是相同的。我的错误是什么?

ADDED: Try to check first and second code (and outputs) before answering.

添加:在回答之前尝试检查第一个和第二个代码(和输出)。

采纳答案by hakre

'0xD'is a string. The documentation clearly specifieshow a string is "casted" into an integer value (picking the (int) '0xD';example here):

'0xD'是一个字符串。该文档清楚地说明了如何将字符串“转换”为整数值(在(int) '0xD';此处选择示例):

When a string is evaluated in a numeric context, the resulting value and type are determined as follows.

If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float.

The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.

当在数字上下文中计算字符串时,结果值和类型按如下方式确定。

如果字符串不包含任何字符 '.'、'e' 或 'E' 并且数值符合整数类型限制(由 PHP_INT_MAX 定义),则该字符串将被评估为整数。在所有其他情况下,它将被评估为浮点数。

该值由字符串的初始部分给出。如果字符串以有效的数字数据开头,这将是使用的值。否则,该值将为 0(零)。有效的数字数据是一个可选的符号,后跟一个或多个数字(可选地包含小数点),后跟一个可选的指数。指数是“e”或“E”后跟一位或多位数字。

The initial porition of the string '0xD'that is a number is 0, hence the it's (int) 0.

'0xD'作为数字的字符串的初始部分是0,因此它是(int) 0

Don't mix this with writing code. If you write that in code it's not a string, it's the hexa-decimal representation/notation of an integer number (Demo):

不要将其与编写代码混为一谈。如果您在代码中编写它,则它不是字符串,而是整数的十六进制表示/表示法(Demo):

<?php

$value = eval("return 0xD;");

var_dump($value); # int(13)

This does not fully answer why the expression '0xD' * 1results in (int) 13, but explains everything else so far and which two rules of integer value interpretation apply in PHP.

这并没有完全回答为什么表达式'0xD' * 1结果为(int) 13,但解释了到目前为止的所有其他内容以及在 PHP 中适用的两个整数值解释规则。

I assume that there is a slight difference between casting to integer and integer context in PHP. If the string representing a hexadecimal number it is used in integer/float context:

我认为在 PHP 中转换为整数和整数上下文之间存在细微差别。如果表示十六进制数的字符串在整数/浮点上下文中使用:

'0xD' * 1

In this expression, the string '0xD'will get evaluated as number because of the *operator and the 1operand (see Type Juggling). This is not Type Casting, because there is no cast language construct.

在此表达式中,'0xD'由于*运算符和1操作数(请参阅类型杂耍),字符串将被评估为数字。这不是Type Casting,因为没有 cast 语言构造。

It looks like that in this expression's case, the interpretation of the string as an integer is done literally(as specified) but is not the same as when you do a conversion.

看起来在这个表达式的情况下,将字符串解释为整数是按字面意思完成的(如指定的那样),但与进行转换时不同。

One could argue it remains undocumented when conversion applies and when literal interpretation applies. However, using type casting can get you around any such problems (Demo):

人们可能会争辩说,当转换适用时和字面解释适用时,它仍然没有记录。但是,使用类型转换可以解决任何此类问题(Demo):

<?php

$value = '0xD';

var_dump($value); # string(3) "0xD"
var_dump($value * 1); # int(13)
var_dump((int) $value * 1); # int(0)

回答by Pelshoff

It's the quotes, apparantly.

显然是引号。

<?php
var_dump('0xD' * 1 );
var_dump((int) 0xD);
var_dump(intval(0xD));
var_dump((float) 0xD);

gives

int(13)
int(13)
int(13)
float(13)

I think PHP does not see the hex values in the strings as possible ints.

我认为 PHP 没有将字符串中的十六进制值视为可能的整数。