php如果某事等于x,y或z

时间:2014-12-26 19:21:35

标签: php if-statement

我正在编写一些php逻辑,我正在尝试简化某些事情。

是否可以编写如下内容:

<?php if( (get_theme_mod('header_image_location')=='x', 'y' or 'z' ) {?> 
    //Do something 
<?php } ?>

我必须这样做:

<?php if( (get_theme_mod('header_image_location')=='x') ||  (get_theme_mod('header_image_location')=='y') || (get_theme_mod('header_image_location')=='z') ) {?> 
    //Do something 
<?php } ?>

只想知道我是否可以简化最底层的例子。感谢

2 个答案:

答案 0 :(得分:4)

创建一个值数组,然后使用in_array

<?php if( in_array(get_theme_mod('header_image_location'), array('x', 'y', 'z'))){?> 
    //Do something 
<?php } ?>

答案 1 :(得分:3)

只需使用in_array()

即可
if (in_array(get_theme_mod('header_image_location'), array('x','y','z'))) {

}
相关问题