【问题标题】:A string in PHP that doesn't make sensePHP 中没有意义的字符串
【发布时间】:2014-10-18 03:50:53
【问题描述】:

我正在试验 PHP 的弱/动态类型属性以准备测试,并且完全被这个字符串连接的输出所迷惑。有人能解释一下这怎么可能吗?

<?php echo  1 . "/n" . '1' + 1 ?><br />

输出:

2

【问题讨论】:

  • 看看echo intval("1/n1");之后是否更有意义,再看看PHP's info on type juggling
  • 哈哈,我认为这不会更有意义,但它会显示解决方案。
  • @skj3gg 我也在想同样的事情——至少废话被记录下来了。
  • 这给出了预期的输出。

标签: php dynamic-typing weak-typing


【解决方案1】:

分析:

echo  1 . "/n" . '1' + 1;

等价于

//joined first 3 items as string
echo "1/n1"+1;

等价于

//php faces '+' operator, it parses '1/n1' as number
//it stops parsing at '/n' because a number doesn't
//contain this character
echo "1"+1;

等价于

echo 1+1;

【讨论】:

    猜你喜欢
    • 2021-08-16
    • 2015-03-08
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-05
    • 1970-01-01
    • 2011-05-08
    相关资源
    最近更新 更多