Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php if something equals x, y or z

I am writing out some php logic, and I'm trying to simplify something.

Is it possible to write something like the following:

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

I must I do it like this:

<?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 } ?>

Just want to know if I can simplify the bottom example. Thanks

like image 545
Sam Skirrow Avatar asked Oct 23 '25 11:10

Sam Skirrow


2 Answers

Make an array of values, then use in_array.

<?php if( in_array(get_theme_mod('header_image_location'), array('x', 'y', 'z'))){?> 
    //Do something 
<?php } ?>
like image 103
Rocket Hazmat Avatar answered Oct 26 '25 00:10

Rocket Hazmat


Just use in_array()

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

}
like image 34
John Conde Avatar answered Oct 26 '25 02:10

John Conde