2012-04-15 33 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'; 
?>