php 上的奇怪行为

发布于 2024-08-01 20:56:01 字数 825 浏览 6 评论 0原文

有人可以告诉我为什么运行以下代码会得到奇怪的结果吗?

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

丿*梦醉红颜 2024-08-08 20:56:01

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.

空心空情空意 2024-08-08 20:56:01

删除前导零,因为它使 PHP 将数字视为八进制数。

Remove the leading zero, because it makes PHP treat the number as an octal number.

尽揽少女心 2024-08-08 20:56:01

由于初始为零,它被解释为八进制数。

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.

请爱~陌生人 2024-08-08 20:56:01

自动碱基转换。 你甚至不需要所有的类代码来查看它的实际效果,

echo 027447002;

问题是 027447002,就数字而言,是八进制(基数 8) - 不是 一个零填充的十进制(基数 - 10) 整数。

Automatic base conversion. You don't even need all that class code to see this in action

echo 027447002;

The thing is that 027447002, in terms of numbers, is octal (base-8) - not a zero-filled decimal (base-10) integer.

云淡风轻 2024-08-08 20:56:01

带有前导零的数字文字是您以八进制(基数 8)指定某些内容的方式。 如果您编写 27447002 而不是 027447002 就可以了。

Numeric literals with leading zeroes are how you specify something in octal (base 8). If you write 27447002 instead of 027447002 you'll be fine.

滥情稳全场 2024-08-08 20:56:01

除了其他人所说的之外,我还有一个评论。

看起来您想要一个 0 填充的数字,对吧? 左边补零的九位数字?

考虑使用 str_pad 函数。 像这样:

...
function bank($name, $id, $bal=0)
{
 ...
 $this->id = str_pad($id, 9, '0', STR_PAD_LEFT);
 ...
}
...

然后在你的函数中你可以调用:

$b = new bank('John Doe', 12345678, 1.25);

如果你输出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:

...
function bank($name, $id, $bal=0)
{
 ...
 $this->id = str_pad($id, 9, '0', STR_PAD_LEFT);
 ...
}
...

Then in your function you can call:

$b = new bank('John Doe', 12345678, 1.25);

If you output id, it would be padded

012345678

昇り龍 2024-08-08 20:56:01

正如每个人都正确地说的那样 - 默认情况下这被认为是八进制值。 我认为类构造函数应该测试它是否是一个有效的整数,并且启动该类应该 类型化值...

function bank($name, $id, $balance=0)
{
    if (is_int($id))
      $this->ID=$id;
    else
      throw new Exception('ID is not integer');

    // and validate these too!
    $this->balance=$balance;
    $this->name=$name;
}

$b= new bank('yaniv', (int)027447002, 15000);

希望有帮助!

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...

function bank($name, $id, $balance=0)
{
    if (is_int($id))
      $this->ID=$id;
    else
      throw new Exception('ID is not integer');

    // and validate these too!
    $this->balance=$balance;
    $this->name=$name;
}

$b= new bank('yaniv', (int)027447002, 15000);

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文