Jump to content

Comparison of 1 variable to multiple values


phpdolan

Recommended Posts

When writing a regex, it is possible to use the pipe ( | ) to signify 'choices'. Evidentally that doesn't work in a comparison.

So if I want to check to see if:

 

$day_of_month equals Jan 1 or Mar 1 or May 1, etc, is there a shorthand? Or do I have to write each like this:

 

if ( $day_of_month == 'Jan 1' OR $day_of_month == 'Mar 1' OR $day_of_month == 'May 1' )

 

I know my options are to do that OR put the values in an array OR to set up a CASE expression, but my question is about a cleaner expression with something that resembles the pipe in a regex?

 

Thanks for all comments,

David

Technically you can still use the pipe :P

 

<?php
if ( $day_of_month == 'Jan 1' || $day_of_month == 'Mar 1' || $day_of_month == 'May 1' )
?>

 

the OP meant a regex pipe:

if(preg_match('/^(Jan 1|Mar 1|May 1)$/',$day_of_month))

Many thanks Aaron.

 

i usually do:

if(in_array($day_of_month,array('Jan 1','Mar 1','May 1')))

 

I will settle on that as the solution.

 

My comment on the joke is Yeah!!! Thanks for the reply. I didn't get the joke, but did get that the double pipe will shorten my typing by ONE character each time.

 

Yeah!!

 

David

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.