2017-01-04 168 views
0

我已经创建了一个函数,用于检查图像是否为空或图像变量没有值或图像未找到,然后返回默认图像,但是在某些产品上它会给出结果但不是所有他们..如果语句没有返回正确的结果

function image_check($image) 
{ 
    $no_image = "noimagefound.jpg"; 
    if(isset($image) || !empty($image) || $image != " ") 
    { 
     if(file_exists('uploads/store/products/'.$image)) 
     { 
      return 'uploads/store/products/'.$image; 
     } 
     else 
     { 
      return 'uploads/web_service/'.$no_image; 
     } 
    } 
    else 
    { 
     return 'uploads/web_service/'.$no_image; 
    } 
} 

任何人都可以使它正常工作吗?我错过了什么?

+2

这没有任何意义......如果它是空的或仅包含空白,则使用'$ image'值... – arkascha

+0

尝试打印'$ image'以确保它包含您所期望的内容。 –

+1

看起来像你想''空($图像)&& $图像!=“”'为条件 – bcmcfc

回答

1
function image_check($image) 
{ 
    $no_image = "noimagefound.jpg"; 
    if(!empty($image) && file_exists('uploads/store/products/'.$image)) 
    { 
     return 'uploads/store/products/'.$image; 
    } 
    return 'uploads/web_service/'.$no_image; 
} 

当他们在评论中指出的那样,你的条件失败,因为你检查,如果它是空的没有,如果它是不是空的。 isset()和!empty()在这种情况下是冗余的。

你也不需要所有其他检查。小心使代码复杂化超出您的需要。你只需要一张支票,如果失败了,然后返回你的$no_image

+0

This works!非常感谢!我现在明白了!欣赏这个解释 – Alexu

+0

循环漏洞: - https://eval.in/709494 –

+0

@Anant没有真正的循环漏洞,因为file_exists调用将失败... – Devon

相关问题