Php 为什么这种情况总是发生在其他情况下?
我有以下条件:Php 为什么这种情况总是发生在其他情况下?,php,cakephp,if-statement,conditional-statements,Php,Cakephp,If Statement,Conditional Statements,我有以下条件: if(!empty($numerofiles)) { // <-- WHEN THIS ARRAY IS EMPTY for ($i=0; $i < $numerofiles; $i++) { $allowed = array('doc','pdf','jpg','jpeg','xls','docx','xlsx'); $ext = pathinfo(
if(!empty($numerofiles)) { // <-- WHEN THIS ARRAY IS EMPTY
for ($i=0; $i < $numerofiles; $i++) {
$allowed = array('doc','pdf','jpg','jpeg','xls','docx','xlsx');
$ext = pathinfo($name, PATHINFO_EXTENSION);
// BLA BLA BLA SOME CODE
if(in_array($ext,$allowed)) {
// BLA BLA SOME CODE
} else { // <-- ..IT SEND ME FOR THIS ELSE
$this->Comment->delete($this->Comment->id);
$this->Session->setFlash('Tipo de arquivo não permitido', 'default', array('class' => 'flash_fail'));
$this->redirect(array('action' => 'view', $id));
}
}
} else { // <-- BUT THIS SHOULD TO GO HERE
}
$this->Session->setFlash('Comentário adicionado com sucesso!', 'default', array('class' => 'flash_sucess'));
$this->redirect(array('action' => 'view', $id));
if(!empty($numerofile)){//delete($this->Comment->id);
$this->Session->setFlash('Tipo de arquivo não permitito','default',array('class'=>'flash_fail');
$this->redirect(数组('action'=>'view',$id));
}
}
}else{//Session->setFlash('Comentário adicionado com successo!','default',array('class'=>'flash_success');
$this->redirect(数组('action'=>'view',$id));
当我的第一个if
,$numerofiles
为空时,这将向我发送第二个if
的else
。
但是,如果$numerofiles
是空的
,我希望它跳转并发送代码结尾。
怎么了?很简单
将if(!empty($numerofile))
更改为if(empty($numerofile))
您正在检查是否为空,而需要检查的是是否为空
另外,您说empty($numeriofiles)
evaluate为false。这意味着它不是空的。然后再次放置not运算符,因此您对它进行了否定,现在的意思是“如果它是空的”
函数的作用是:检查变量是否为空
Return value
FALSE if var_name has a non-empty and non-zero value.
Value Type : Boolean
List of empty things :
"0" (0 as a string)
0 (0 as an integer)
"" (an empty string)
NULL
FALSE
"" (an empty string)
array() (an empty array)
$var_name; (a variable declared but without a value in a class)
编辑:
看来你还没有从这个问题中走出来
让我再试一次
第1步:if(!empty($numerofile))
=>if(NOT(文件数为空))
这里的关键是的布尔值(文件数为空)
在您的评论中,您说empty($numerofile)
计算为false
所以(文件数为空)为FALSE
再次查看步骤1:
if(非(假))
=>if(真)
所以你的实际代码,对于这个场景,对于当前的输入,
而执行是if(TRUE)
由于它是if(TRUE)
,控件进入内部。同意吗?$ext和$allowed是什么样子的?$ext应该是值,$allowed应该是数组,只需确保知道什么是空($numerofiles)
返回?TRUE还是false?@prash$numerofiles是空的!!(!empty($numerofiles)
计算结果为true,控制进入if语句。我想你不需要not运算符“!
”。你明白吗?不。如果$numerofiles
不是空的,我只想运行if
。但如果是空的,这就是运行elseinside@Igor马丁斯:这是不可能的。@IgorMartins你的问题很糟糕,我们会的ant甚至复制了你的问题,因为你的代码并不相关,他很困惑。这很好。看到你说的“但当它为空时,这是运行内部的else”。但是你也说,empty($numeriofiles)
求值为false,意味着numberoffiles不是空的。因此您认为它是空的,但实际上它不是空的。如果它是空的,empty($numerofiles)
必须求值为true。@Igor Martins:检查并提供相应的代码段。否则您将浪费时间猜测