switch($_SERVER["SCRIPT_NAME"]){
case "/index.php":
$this->pageId = 1;
break;
case "/shops/index.php":
$this->pageId = 2;
break;
case "/shops/dailydeals.php":
$this->pageId = 4;
break;
case "/shops/shops.php":
$this->pageId = 5;
break;
case "/shops/deals.php":
$this->pageId = 9;
break;
case "/shops/store.php":
$this->pageId = 10;
break;
case "/user/cashmail.php":
$this->pageId = 13;
break;
case "/user/cashmail.php":
$this->pageId = 13;
break;
default ;
$this->pageId = 1;
break;
}
Run Code Online (Sandbox Code Playgroud)
*您对上述代码的看法是正确的还是每个案例都应该用大括号写成?我们可以在没有花括号的情况下编写案例(不超过两行)吗?如果可以,使用或不使用花括号之间是否存在性能差异?感谢帮助.*
php syntax performance switch-statement conditional-statements
我有一个foreach循环,只有在'Valid'属性设置为true时才需要迭代.不幸的是,如果列表中第一项的'Valid'设置为false,它将退出整个循环.
有没有人知道在foreach循环中使用条件的最佳方法?以下是我现在所拥有的.
foreach (var course in agentNewTraining.AllCoursesTaken.TakeWhile(c => c.Valid))
Run Code Online (Sandbox Code Playgroud) 使用python 2.7.4
让我们说我有一个清单
list = ['abc', 'def']
Run Code Online (Sandbox Code Playgroud)
我想找到它是否包含某些东西.所以我尝试:
[IN:] 'abc' in list
[OUT:] True
[IN:] 'def' in list
[OUT:] True
[IN:] 'abc' and 'def' in list
[OUT:] True
Run Code Online (Sandbox Code Playgroud)
但是当我list.pop(0)并重复上一次测试:
[IN:] 'abc' and 'def in list
[OUT:] True
Run Code Online (Sandbox Code Playgroud)
即使:
list = ['def']
Run Code Online (Sandbox Code Playgroud)
谁知道为什么?
我试图实现这个逻辑,我只是想知道是否有更好的方法?
if(condition1) {
do1();
}
else if(condition2) {//especially here that I have to check condition3 here and also in the last else if
do2();
if(condition3) {
do3();
}
}
else if(condition3) {
do3();
}
Run Code Online (Sandbox Code Playgroud) 这是意味着什么:在提示用户之后,"你想输入另一个名字吗?" 如果用户输入"Y",则应提示他们添加其他名称.相反,程序运行在cin << response之后开始的循环,我认为我将条件设置为仅在用户未输入'Y','y,'N,'或'n'时运行.实际上,无论用户如何回答该问题,该程序似乎都与我想要的相反.
#include <iostream>
#include <cstring>
using namespace std;
int main(){
const int MAX_NUM = 101;
char name[MAX_NUM];
char response;
char nameCorrect = 'n';
double total = 0;
do{
do{
cout << "Please enter name: ";
cin >> name;
cin.ignore(100, '\n');
cout << "It looks like you entered " << name
<< ". Is this correct? (Y/N) " << endl;
cin >> nameCorrect;
while (nameCorrect != 'y' & nameCorrect != 'Y' & nameCorrect != 'n' & nameCorrect != …Run Code Online (Sandbox Code Playgroud) 所以我有以下程序:
# define swap(a,b) temp=a; a=b; b=temp;
int main() {
int i, j, temp;
i = 5;
j = 10;
temp = 0;
if (i > j)
swap(i, j);
printf("%d %d %d", i, j, temp);
}
Run Code Online (Sandbox Code Playgroud)
这导致:
10, 0, 0
Run Code Online (Sandbox Code Playgroud)
我不明白的是为什么if (5 > 10)条件被执行为"真",即使5不大于10.
我想做的是只运行一次if语句并禁用该语句。在我的代码中,如果我连续单击按钮,则if语句仍会呈现。这是为什么?
HTML:
<button onclick="appendMsg()">submit</button>
<div id="wrapper"></div>
Run Code Online (Sandbox Code Playgroud)
Javascript:
function appendMsg(){
var triggerOnceforShare = true;
if(triggerOnceforShare){
triggerOnceforShare = false;
document.getElementById('wrapper').innerHTML+=triggerOnceforShare + "<br />"
console.log(triggerOnceforShare);
}
}
Run Code Online (Sandbox Code Playgroud)
codepen:http://codepen.io/vincentccw/pen/uoBkI
我的查找条件似乎没有进入基于Debugkits SQL日志的SQL.有人能向我指出我做错了什么吗?这是我第一次尝试过滤查找结果.
BillingCenters控制器
$conditions = array(
'BillingCenter.isactive' => '1'
);
$this->set(
'billingcenters',
$this->BillingCenter->find('all', array($conditions))
);
Run Code Online (Sandbox Code Playgroud)
DebugKit显示的结果SQL查询是..(缺少WHERE子句???)
SELECT
`BillingCenter`.`id`,
`BillingCenter`.`startdate`,
`BillingCenter`.`enddate`,
`BillingCenter`.`name`,
`BillingCenter`.`isactive`,
`BillingCenter`.`created`,
`BillingCenter`.`modified`
FROM
`bm`.`billing_centers` AS `BillingCenter`
WHERE
1 = 1
Run Code Online (Sandbox Code Playgroud)
如果我将控制器代码更改为
$this->set(
'billingcenters',
$this->BillingCenter->find('all')
);
Run Code Online (Sandbox Code Playgroud)
生成的SQL仍然是相同的.
SELECT
`BillingCenter`.`id`,
`BillingCenter`.`startdate`,
`BillingCenter`.`enddate`,
`BillingCenter`.`name`,
`BillingCenter`.`isactive`,
`BillingCenter`.`created`,
`BillingCenter`.`modified`
FROM
`bm`.`billing_centers` AS `BillingCenter`
WHERE
1 = 1
Run Code Online (Sandbox Code Playgroud) 这是一个非常挑剔的问题,但好奇心让我变得更好.
每次循环执行时,for循环是否重新评估RHS条件?见下文.
for(int i = 0; i < collection.Count; i++)
{
}
Run Code Online (Sandbox Code Playgroud)
在整个代码库中执行以下操作是否良好?
for(int i = 0, n = collections.Count; i < n; i++)
{
}
Run Code Online (Sandbox Code Playgroud)
或者编译器是否进行了这些优化/它们可以忽略不计(即使有庞大的代码库)?
正在修复我们的代码中的错误,发现这个行为不端的奇怪功能:
private String calculate(String a, String b) {
return a == null ? "" : a + a != null && b != null ? "\n" : "" + b == null ? "" : b;
}
Run Code Online (Sandbox Code Playgroud)
匆匆忙忙,我只是添加了一些括号,让它像这样工作:
private String calculatePar(String a, String b) {
return a == null ? "" : a + (a != null && b != null ? "\n" : "") + (b == null ? "" : b);
}
Run Code Online (Sandbox Code Playgroud)
顺便说一下,这不是最优雅的做法,但两者实际上都来自我编写的(简化的)测试用例示例.
现在我对有缺陷的第一个功能进行了一些测试,这就是我想到的:
if-statement ×3
c# ×2
java ×2
loops ×2
c ×1
c++ ×1
cakephp ×1
collections ×1
evaluation ×1
find ×1
for-loop ×1
foreach ×1
javascript ×1
linq ×1
list ×1
performance ×1
php ×1
python ×1
string ×1
syntax ×1
where ×1
while-loop ×1