我正在编写一些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 } ?>
只想知道我是否可以简化最底层的例子。感谢
答案 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'))) {
}