假设以下内容,我得到了一点:
foreach ($arr as $key => $value) {
switch($key) {
// ... some other cases
default:
continue;
// ^== assumption: move on to the next iteration of the foreach
// actual PHP: treat this continue just like a break
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
但实际上,根据继续的文档:
为了继续,switch语句被认为是一个循环结构.
PHP语言设计者有这个选择的理由吗?据我所知,switch 这不是一个循环控制结构,所以为什么在这种情况下将其视为一个?
当我在控制台中运行节点并键入时var _ = require('underscore');,_最终未定义.如果我将相同的代码放在一个文件中并执行它,那么下划线库将按预期包含在内.
$ node
> var _ = require('underscore');
> console.log(_)
undefined // underscore library does not load
> var async = require('async');
undefined
> console.log(async) // async library does
{ noConflict: [Function],
nextTick: [Function],
forEach: [Function],
...
>
Run Code Online (Sandbox Code Playgroud)
但是执行的.js文件中的相同代码node test.js显示两个库按预期加载.这是怎么回事?
我在phpunit.xml文件中有这个:
<phpunit ...>
<testsuites>
<testsuite name="MyTests">
<directory>../path/to/some/tests</directory>
</testsuite>
</testsuites>
... // more settings for <filter> and <logging>
</phpunit>
Run Code Online (Sandbox Code Playgroud)
当我去运行它时,我收到此错误:
PHP fatal error: Uncaught exception 'PHPUnit_Framework_Exception'
with message 'Neither "MyTests.php" nor "MyTests.php" could be opened.'
Run Code Online (Sandbox Code Playgroud)
为什么PHPUnit会给我这个错误,如果我给它一个查找测试的目录,它为什么要查找"MyTests.php"?
在相关的说明中,当我<testsuite>使用其他测试添加更多条目时,PHPUnit运行时没有错误.那是怎么回事?
有很多"为什么PHP会在这里抛出错误?" 的问题.嗯,这有点不同.我在重新编写同事编写的代码时发现了以下代码:
foreach($arr as $key => $value) {http://google.com/
echo $value;
// ...
}
Run Code Online (Sandbox Code Playgroud)
我的第一个想法是:"嗯......多么令人尴尬;他一定是不小心粘贴在那里......"接着是:"等等......这段代码实际上没有办法运行......那应该是语法错误".但是:
$ php -l test.php
No syntax errors detected
Run Code Online (Sandbox Code Playgroud)
事实上,(就像那些看似不应该运行的PHP代码一样)它在生产中运行没有问题.所以我做了一点测试:
foreach($arr as $key => $value) {http://google.com/ <-- original, no error
foreach($arr as $key => $value) {http: <-- also no syntax error
foreach($arr as $key => $value) {http <-- bingo! "Unexpected T_ECHO..."
Run Code Online (Sandbox Code Playgroud)
什么小的PHP语法产生了如此奇怪的结果?
(我使用的是PHP 5.3.5)
我在mongodb有一个大集合(约270万个文档),并且有很多重复.我试着ensureIndex({id:1}, {unique:true, dropDups:true})在集合上运行.在决定之前,Mongo会对它进行一段时间的搅拌too many dups on index build with dropDups=true.
如何添加索引并删除重复项?或者反过来说,删除一些重复的最佳方法是什么,以便mongo可以成功构建索引?
对于奖励积分,为什么可以删除的重复次数有限制?