2012-04-15 71 views
0

此代碼:屬性存在

$_post = &get_post($post->ID); 
$classname = ($_post->iconsize[0] <= 128 ? 'small' : '') . 'attachment'; 

有時會產生這樣的錯誤:

[Sun Apr 15 08:51:35 2012] [error] [client 180.76.5.150] PHP Notice: Undefined property: stdClass::$iconsize in /srv/www/virtual/myblog.com/htdocs/wp-content/themes/mimbo/attachment.php on line 8 

我想修改該行添加property_exists在屬性上檢查,並默認爲「」如果它不不存在,但對處理屬性的語法有點不熟悉。線路看起來如何?

+0

[PHP: 「通知:未定義變量」 和 「通知:未定義指數」]的可能重複(HTTP://計算器。 COM /問題/ 4261133/PHP-通知-未定義變量和 - 通知-未定義索引) – animuson 2012-04-15 03:49:14

回答

1

只需使用isset

if(isset($_post->iconsize)) { 
    // ... 
} 

所以:

<?php 
$_post = &get_post($post->ID); 
$classname = (isset($_post->iconsize) && $_post->iconsize[0] <= 128 ? 'small' : '') . 'attachment'; 
?>