在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?

请每个回答只回答一个特征。


当前回答

在PHP中,如下:

<?php $foo = 'abc'; echo "{$foo";

是语法错误。

如果你真的想要{,后面跟着$foo的内容,你必须使用。:

<?php $foo = 'abc'; echo '{' . $foo;

其他回答

PHP

$ php -r '::'
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM

WTF ? http://en.wikipedia.org/wiki/Scope_resolution_operator

为什么不说意外的T_SCOPE_RESOLUTION_OPERATOR ?

早期的FORTRAN,空格不重要。(anti-Python !)

DO 20 I = 1, 10

含义:从这里循环到第20行,I从1到10。

DO 20 I = 1. 10

含义:将1.10分配给名为DO20I的变量。

有传言说这个漏洞毁了一个太空探测器。

PHP对字符串中数值的处理。详见之前对另一个问题的回答,但简而言之:

"01a4" != "001a4"

如果你有两个包含不同数量字符的字符串,它们不能被认为是相等的。前导零很重要,因为它们是字符串而不是数字。

"01e4" == "001e4"

PHP doesn’t like strings. It’s looking for any excuse it can find to treat your values as numbers. Change the hexadecimal characters in those strings slightly and suddenly PHP decides that these aren’t strings any more, they are numbers in scientific notation (PHP doesn’t care that you used quotes) and they are equivalent because leading zeros are ignored for numbers. To reinforce this point you will find that PHP also evaluates "01e4" == "10000" as true because these are numbers with equivalent values. This is documented behaviour, it’s just not very sensible.

从Ruby中的随机类继承:

class RandomSubclass < [Array, Hash, String, Fixnum, Float, TrueClass].sample
   ...
end

(首次出现在Ruby的隐藏特性中)

在Python中:

>>> a[0] = "hello"
NameError: name 'a' is not defined
>>> a[0:] = "hello"
NameError: name 'a' is not defined
>>> a = []
>>> a[0] = "hello"
IndexError: list assignment index out of range
>>> a[0:] = "hello"
>>> a
['h', 'e', 'l', 'l', 'o']

这些切片分配也会给出相同的结果:

a[:] = "hello"
a[42:] = "hello"
a[:33] = "hello"