php 上的奇怪行为
有人可以告诉我为什么运行以下代码会得到奇怪的结果吗?
<?php
class Bank
{
var $ID;
var $balance;
var $name;
function bank($name,$id,$balance=0)
{
$this->ID=$id;
$this->balance=$balance;
$this->name=$name;
}
function getBalance()
{
return $this->balance;
}
function setBalance($bal)
{
$this->balance=$bal;
}
function getId()
{
return $this->ID;
}
function setId($i)
{
$this->ID=$i;
}
)
$b= new bank(yaniv,027447002, 15000);
现在,当我尝试回显:
$b->ID
而不是预期的 027447002 时,我得到了一个奇怪的 6180354, 但如果我像这样启动对象:(
$b=new bank(yaniv,'027447002',15000);
注意我引用了 id 属性)它工作正常。 有什么建议为什么会发生这种情况以及解决它的正确方法是什么?
Can some one please tell me why I get odd results rurning the following code?
<?php
class Bank
{
var $ID;
var $balance;
var $name;
function bank($name,$id,$balance=0)
{
$this->ID=$id;
$this->balance=$balance;
$this->name=$name;
}
function getBalance()
{
return $this->balance;
}
function setBalance($bal)
{
$this->balance=$bal;
}
function getId()
{
return $this->ID;
}
function setId($i)
{
$this->ID=$i;
}
)
$b= new bank(yaniv,027447002, 15000);
Now when I try to echo:
$b->ID
Instead of the expected 027447002 I get an odd 6180354,
but if I initiate the object like this :
$b=new bank(yaniv,'027447002',15000);
(notice I quoted the id property) it works OK.
Any suggestion why is this happening and what is the right way to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
027447002 采用 八进制 格式,因为它以零为前缀。 将其转换为十进制,您将得到 6180354!
有关详细信息,请参阅整数手册页。
027447002 is in octal, as it prefixed with a zero. Convert that to decimal and you get 6180354!
See the manual page on integers for details.
删除前导零,因为它使 PHP 将数字视为八进制数。
Remove the leading zero, because it makes PHP treat the number as an octal number.
由于初始为零,它被解释为八进制数。
http://www.php.net/manual/en/language。 types.integer.php
如果数字在打印时应该用零填充(它们始终是特定的长度),那么您可以使用 sprintf() 将存储的整数转换为零填充字符串。
Because of the initial zero, it is interpreted as an octal number.
http://www.php.net/manual/en/language.types.integer.php
If the number should be left padded with zeros when printed (they are always a specific length) then you can use sprintf() to convert the stored integer to a zero padded string.
自动碱基转换。 你甚至不需要所有的类代码来查看它的实际效果,
问题是 027447002,就数字而言,是八进制(基数 8) - 不是 一个零填充的十进制(基数 - 10) 整数。
Automatic base conversion. You don't even need all that class code to see this in action
The thing is that 027447002, in terms of numbers, is octal (base-8) - not a zero-filled decimal (base-10) integer.
带有前导零的数字文字是您以八进制(基数 8)指定某些内容的方式。 如果您编写
27447002
而不是027447002
就可以了。Numeric literals with leading zeroes are how you specify something in octal (base 8). If you write
27447002
instead of027447002
you'll be fine.除了其他人所说的之外,我还有一个评论。
看起来您想要一个 0 填充的数字,对吧? 左边补零的九位数字?
考虑使用 str_pad 函数。 像这样:
然后在你的函数中你可以调用:
如果你输出id,它将被填充
012345678
I have one comment besides what everyone else is saying.
It appears you want a 0 padded number, right? A nine digit number that's padded with zeros on the left?
Think about using str_pad function. Like so:
Then in your function you can call:
If you output id, it would be padded
012345678
正如每个人都正确地说的那样 - 默认情况下这被认为是八进制值。 我认为类构造函数应该测试它是否是一个有效的整数,并且启动该类应该 类型化值...
希望有帮助!
As everyone rightly said - this is considered an octal value by default. I think the class constructor should be testing that it is a valid integer, and initiating the class should typecast the value...
Hope that helps!