我有一个foreach循环和一个if语句.如果发现匹配,我需要最终打破foreach.
foreach($equipxml as $equip) {
$current_device = $equip->xpath("name");
if ( $current_device[0] == $device ) {
// found a match in the file
$nodeid = $equip->id;
<break out of if and foreach here>
}
}
Run Code Online (Sandbox Code Playgroud) 我了解“ finally”关键字在各种语言中的含义,但是,我很难理解为什么您会在口味偏爱格式之外使用它。
例如,在PHP中:
try {
possibleErrorThrownFunction();
}
catch (CustomException $customException) {
// handle custom error
}
catch (Exception $exception) {
// handle the error
}
finally {
// run this code every single time regardless of error or not
}
Run Code Online (Sandbox Code Playgroud)
这段代码在做什么和这样做之间有什么区别?
try {
possibleErrorThrownFunction();
}
catch (CustomException $customException) {
// handle custom error
}
catch (Exception $exception) {
// handle the error
}
// run this code every single time regardless of error or not
Run Code Online (Sandbox Code Playgroud)
由于捕获了错误,最后一行不是总会运行吗?在这种情况下,finally
除非您只想维护代码样式的格式,否则没有实际使用的情况? …
我在做什么 :
create table sample (id INT(10) PRIMARY KEY AUTO_INCREMENT,name varchar(255),marks INT(10));
insert into sample (name,marks) VALUES('sam',10);
insert into sample (name,marks) VALUES('sam',20);
insert into sample (name,marks) VALUES('sam',NULL);
insert into sample (name,marks) VALUES('sam',NULL);
insert into sample (name,marks) VALUES('sam',30);
select AVG(marks) from sample GROUP BY(name);
Run Code Online (Sandbox Code Playgroud)
产出我的预期:
AVG =(10 + 20 + 30)/ 5 = 12
MYSQL的输出:
AVG =(10 + 20 + 30)/ 3 = 20
理想情况下我想要的是MYSQL应该得到5行的总和并除以5,但它只能除以3(非NULL行)
为什么会发生这种情况,我该怎么做才能获得正确的AVG即60/5?PS:我不能使标记字段为NOT NULL,在我的db设计中,标记字段允许为NULL.
谢谢
这行代码适用于Apple.com的导航栏
#globalheader #globalnav[class*="nosearch"] { width:100%; }
Run Code Online (Sandbox Code Playgroud)
有人知道课后的星号是什么意思吗?
我需要为用户创建一个唯一的ID.我不想使用auto_incrementing id,因为我不希望用户能够猜出我们拥有多少用户,或者增长率是多少.
UUID也不是一个选项,因为用户必须在智能手机上重新输入ID.
所以我希望我能尽可能地减少"强制"数据库以找到未使用的ID.什么是一个聪明的方法来解决这个问题?
谢谢!
我有一个字符串和开始和长度,用于提取子字符串.两个位置(开始和长度)都基于原始UTF8字符串中的字节偏移量.
但是,有一个问题:
开始和长度以字节为单位,因此我不能使用"substring".UTF8字符串包含多个多字节字符.这样做是否有超高效的方法?(我不需要解码字节......)
示例:var orig ='你好吗?'
s,e可能是3,3来提取第二个字符(好).我在找
var result = orig.substringBytes(3,3);
Run Code Online (Sandbox Code Playgroud)
救命!
更新#1在C/C++中,我只是将其转换为字节数组,但不确定javascript中是否存在等价物.顺便说一句,是的,我们可以将它解析成一个字节数组并将其解析回一个字符串,但似乎应该有一个快速的方法在正确的地方剪切它.想象一下'orig'是1000000个字符,s = 6个字节,l = 3个字节.
更新#2感谢zerkms有用的重定向,我最终得到了以下内容,它不能正常工作 - 适用于多字节但是混乱单字节.
function substrBytes(str, start, length)
{
var ch, startIx = 0, endIx = 0, re = '';
for (var i = 0; 0 < str.length; i++)
{
startIx = endIx++;
ch = str.charCodeAt(i);
do {
ch = ch >> 8; // a better way may exist to measure ch len
endIx++;
}
while (ch);
if (endIx > …
Run Code Online (Sandbox Code Playgroud) 我目前正在制作一款在线多人棋类游戏.我想知道用户触发器是否适合登录每个动作.
然后,使用nodejs,我会重复检查触发器表并更新玩家和观察者的视觉效果.
由于我只需要在数据库中进行更改,因此可视化方面将自动跟进(使用重复功能检查数据库中已更改的数据).这样做的目的是将视觉效果与制作游戏所涉及的逻辑分开.
你们中的任何一个人都会推荐这种技术吗?
我在MySQL中创建了这样的表:
DROP TABLE IF EXISTS `barcode`;
CREATE TABLE `barcode` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`code` varchar(40) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
INSERT INTO `barcode` VALUES ('1', 'abc');
INSERT INTO `barcode` VALUES ('2', 'abc ');
Run Code Online (Sandbox Code Playgroud)
然后我从表条形码查询数据:
SELECT * FROM barcode WHERE `code` = 'abc ';
Run Code Online (Sandbox Code Playgroud)
结果是:
+-----+-------+
| id | code |
+-----+-------+
| 1 | abc |
+-----+-------+
| 2 | abc |
+-----+-------+
Run Code Online (Sandbox Code Playgroud)
但我希望结果集只有1条记录.我解决了:
SELECT * FROM barcode WHERE `code` …
Run Code Online (Sandbox Code Playgroud) 这个想法就是说你有下表.
-------------
| oID | Area|
-------------
| 1 | 5 |
| 2 | 2 |
| 3 | 3 |
| 5 | 3 |
| 6 | 4 |
| 7 | 5 |
-------------
Run Code Online (Sandbox Code Playgroud)
如果按连续性分组,则可以使用此伪查询
SELECT SUM(Area) FROM sample_table GROUP BY CONTINUITY(oID)
Run Code Online (Sandbox Code Playgroud)
会回来的
-------------
| SUM(Area) |
-------------
| 10 |
| 12 |
-------------
Run Code Online (Sandbox Code Playgroud)
如果在oID处出现连续性中断,或者缺少连续性中断,则表示oID 4.
这些功能是否存在于Sql的标准功能中?
我正面临Zend Frameworks flashMessenger一个非常奇怪的问题,无法找出问题的原因,也不知道如何解决问题.
当请求的操作需要很长时间时,flashMessenger无法按预期工作.
非工作示例:
class AttachmentController extends Zend_Controller_Action {
public function printAction() {
// action takes really long and causes flash message to not appear at all
sleep(11);
$this->_helper->flashMessenger
->setNamespace('success')
->addMessage("It's done!");
$this->_redirect('/attachment/index');
}
}
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,后面的控制器操作/attachment/index
不会显示flashMessage.
但是,如果我减少脚本的运行时间,它的工作原理:
class AttachmentController extends Zend_Controller_Action {
public function printAction() {
// now the action runs faster and the flashMessage appears!
sleep(1);
$this->_helper->flashMessenger
->setNamespace('success')
->addMessage("It's done!");
$this->_redirect('/attachment/index');
}
}
Run Code Online (Sandbox Code Playgroud)
问题:flashMessenger不显示我的消息的原因是什么?我怎么能解决这个问题?
笔记:
sleep(11)
生产代码,它产生了描述的行为.问题不是由于我为了隔离案例而替换的代码引起的.它真的是由脚本的长运行时间引起的.sleep(11) …