我创建了一个函数,用于检查图像是否为空或图像变量没有值,或者图像未找到,然后返回默认图像,但在某些产品上,它会给出结果,但不会给出所有图像。
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;
}
}
任何人都能让它正常工作吗?我错过了什么?
答案 0 :(得分: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
。