将字符串转换为int [英] casting string to int

查看:92
本文介绍了将字符串转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码

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

实际结果:

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

为什么前两个条件的结果不相同?能给我提供正确的文档吗?

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

我发现的全部是

  • 类型转换:类型PHP中的强制转换与C中的强制转换一样有效:所需类型的名称在要强制转换的变量之前用括号括起来.在某些类型之间进行转换时,究竟会发生什么并不完全清楚.有关更多信息,请参见以下部分:...
  • 转换为整数:从字符串: 请参阅将字符串转换为数字
  • 将字符串转换为数字在数字上下文中评估字符串时,结果值和类型确定如下.
  • 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 numbers When a string is evaluated in a numeric context, the resulting value and type are determined as follows.

据我所知,文档显示直接转换为int并通过放入数字上下文应相同.我怎么了?

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?

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

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

推荐答案

'0xD'是一个字符串. 文档明确规定了如何字符串被投射"到字符串中.转换为整数值(在此处选择(int) '0xD';示例):

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

在数字上下文中评估字符串时,结果值和类型的确定如下.

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

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

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.

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

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.

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

The initial porition of the string '0xD' that is a number is 0, hence the it's (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):

<?php

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

var_dump($value); # int(13)

这不能完全回答为什么表达式'0xD' * 1产生(int) 13的原因,但是可以解释到目前为止的所有其他内容以及整数值解释的哪两个规则在PHP中适用.

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

我假设在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:

'0xD' * 1

在此表达式中,由于*运算符和1操作数,字符串'0xD'将作为数字求值(请参见

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

在此表达式的情况下,将字符串解释为整数

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):

<?php

$value = '0xD';

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

这篇关于将字符串转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆