在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
在找函数吗?为什么不是一门语言呢?
我喜欢PHP,但它总是这样构建:“哦,糟了!我忘了这个!让我们在函数"中添加另一个参数,结果如下:
Str_replace ($search, $replace, $subject…) Strstr ($subject, $search,…)
注意额外的下划线和参数的不同顺序。
这里还有一些东西
$a = array( 'a', 'b', 'c', 'd');
print_r($a); //Prints array( 0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd');
unset($a[2]); //Destroys the element 2 of the list
print_r($a); //Prints array( 0 => 'a', 1 => 'b', 3 => 'd');
其他回答
在SQL server(至少MS)中:
这将总是求值为false:
IF @someint <> NULL
考虑到:
DECLARE @int INT
SET @int = 6
IF @int <> NULL
BEGIN
Print '@int is not null'
END
ELSE
BEGIN
Print '@int is evaluating to null'
END
输出将是:
@int is evaluating to null
必须这样写:
IF @someint IS NOT NULL
BEGIN
END
谁让英语专业的人加入了SQL队!:)
在JavaScript中,你可以使用双位负(~~n)来代替Math.floor(n)(如果n是正数)或parseInt(n, 10)(即使n是负数)。N | N和N & N总是得到和~~ N相同的结果。
var n = Math.PI;
n; // 3.141592653589793
Math.floor(n); // 3
parseInt(n, 10); // 3
~~n; // 3
n|n; // 3
n&n; // 3
// ~~n works as a replacement for parseInt() with negative numbers…
~~(-n); // -3
(-n)|(-n); // -3
(-n)&(-n); // -3
parseInt(-n, 10); // -3
// …although it doesn’t replace Math.floor() for negative numbers
Math.floor(-n); // -4
单个位的求反(~)计算-(parseInt(n, 10) + 1),因此两个位的求反将返回-(-(parseInt(n, 10) + 1) + 1)。
更新:这里有一个jsPerf测试用例,比较了这些替代方案的性能。
C和c++中的三联体。
int main() {
printf("LOL??!");
}
这将打印LOL|,因为trigraph ??!转换为|。
PHP中的变量
PHP中一个奇怪的特性,它允许你从其他变量的内容中创建和分配变量(警告,未经测试的代码):
$a = 'Juliet';
$$a = 'awesome'; // assigns a variable named $Juliet with value 'awesome'
echo '$a'; // prints Juliet
echo '${$a}'; // prints awesome
echo '$Juliet'; // prints awesome
好吧,假设我们有这样的东西:
$bob = 'I\'m bob';
$joe = 'I\'m joe';
$someVarName = 'bob';
$$someVarName = 'Variable \'bob\' changed';
用各种间接的方式来找点乐子怎么样:
$juliet = 'Juliet is awesome!';
$func = 'getVarName'
echo '${$func()}'; // prints 'Juliet is awesome!'
function getVarName() { return 'juliet'; }
我有点纠结:
1;
在perl中,模块需要返回true。