search

Home  >  Q&A  >  body text

php - Why can't this be calculated and the result always returns 0?

                    <?php
                    $a = $this->item_info['jhl'];
                    $c = intval($a);
                    $b = $this->item_info['price'];
                    $d = intval($b);

                    echo intval((500 / $c) * $d)
                    ; ?>
$this->item_info['jhl']跟$this->item_info['price'];在数据库里面都是数字,如123,456,789这样的数字

为什么计算结果都是0呢

The database field is varchar

为情所困为情所困2799 days ago412

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 13:17:48

    intval(500/intval("789")) = 0
    intval(500/intval("345")) = 1,
    Same as above

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 13:17:48

    When you encounter this kind of problem, print out each variable. The result of using var_dump(),我猜测是因为$c比500大,然后intval()对于浮点数只会取整,比如intval(0.5) is 0

    reply
    0
  • Cancelreply