[英]PHP check IF non-empty, non-zero
我用 getimagesize function 得到圖像的寬度和高度,如下所示:
list($width,$height) = getimagesize($source_pic);
如何使用 IF 條件檢查 getimagesize function 執行時沒有錯誤,並且 $width 和 $height 得到非空、非零值?
if ($size = getimagesize($source_pic)) {
list($width,$height) = $size;
if($height > 0 && $width > 0) {
// do stuff
}
}
if ($width === NULL) {
//handle error
}
如果有錯誤getimagesize
返回FALSE
,而不是數組,因此list
分配將導致變量為NULL
。
這應該足夠了:
list($width, $height) = getimagesize($source_pic);
if( $width>0 && $height>0 ){
// Valid image with known size
}
如果它不是有效圖像,則 $width 和 $height 都將為NULL
。 如果它是有效圖像但 PHP 無法確定其尺寸,則它們將為0
。
手冊中的注釋:
某些格式可能不包含圖像或可能包含多個圖像。 在這些情況下,getimagesize() 可能無法正確確定圖像大小。 在這些情況下,getimagesize() 將為寬度和高度返回零。
$size = getimagesize('image.jpg');
list($height,$width) = getimagesize('image.jpg');
if($height>0 && $width>0){
//it comes into if block if and only if both are not null
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.