我有这块代码不能正确执行,它让我疯狂!为什么first if语句总是执行?
private function verifyImage() {
if (!is_null($this->uploads) && array_key_exists('image', $this->uploads)) {
$image = $this->uploads['image'];
$tmpPath = $image['tmp_name'];
if (!empty($tmpPath)) {
$newName = $this->userName . "." . pathinfo($image['name'],PATHINFO_EXTENSION);
move_uploaded_file($tmpPath, __ROOT__ . '/images/' . $newName);
$this->image = __WEBROOT__ . '/images/' . $newName;
}
} elseif (isset($this->formInput['currentImage'])) {
$this->image = trim($this->formInput['currentImage']);
} elseif (isset($this->formInput['image']) && !empty($this->formInput['image'])) {
$this->image = trim($this->formInput['image']);
} else {
$this->setError('image',"Error with image field");
}
}
$这个 - >上传为$从HTML后_FILES
$这个 - >的formInput是$ _ POST从HTML后
问题是与隐藏字段 'currentImage',萤火虫表演它是明确的。然而,最后的else循环是被设置的。它仅适用于$ _ POST返回true [ 'currentImage']正在如果我改变的代码如下设置:
来自:} elseif (isset($this->formInput['currentImage'])) {
到:} if (isset($this->formInput['currentImage'])) {
所以ELSEIF或 '否则,如果' 返回false,但简单的如果返回true?
这听起来像第一条件为真,那么它甚至不检查其他条件 – dave 2014-12-07 06:33:22
你的第一个if返回true,所以elseif永远不会被执行。 – MisterBla 2014-12-07 06:33:38
我建议通过缓存'isset'操作的结果来简化代码,使其更具可读性。 – Dai 2014-12-07 06:33:48