phpdolan Posted January 23, 2009 Share Posted January 23, 2009 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 Link to comment https://forums.phpfreaks.com/topic/142129-comparison-of-1-variable-to-multiple-values/ Share on other sites More sharing options...
rhodesa Posted January 23, 2009 Share Posted January 23, 2009 i usually do: if(in_array($day_of_month,array('Jan 1','Mar 1','May 1'))) Link to comment https://forums.phpfreaks.com/topic/142129-comparison-of-1-variable-to-multiple-values/#findComment-744425 Share on other sites More sharing options...
flyhoney Posted January 23, 2009 Share Posted January 23, 2009 Technically you can still use the pipe <?php if ( $day_of_month == 'Jan 1' || $day_of_month == 'Mar 1' || $day_of_month == 'May 1' ) ?> Link to comment https://forums.phpfreaks.com/topic/142129-comparison-of-1-variable-to-multiple-values/#findComment-744428 Share on other sites More sharing options...
rhodesa Posted January 23, 2009 Share Posted January 23, 2009 Technically you can still use the pipe <?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)) Link to comment https://forums.phpfreaks.com/topic/142129-comparison-of-1-variable-to-multiple-values/#findComment-744433 Share on other sites More sharing options...
flyhoney Posted January 23, 2009 Share Posted January 23, 2009 I know I was making a joke. A very bad joke obviously. Link to comment https://forums.phpfreaks.com/topic/142129-comparison-of-1-variable-to-multiple-values/#findComment-744450 Share on other sites More sharing options...
rhodesa Posted January 23, 2009 Share Posted January 23, 2009 I know I was making a joke. A very bad joke obviously. oh...in that case... Link to comment https://forums.phpfreaks.com/topic/142129-comparison-of-1-variable-to-multiple-values/#findComment-744452 Share on other sites More sharing options...
Maq Posted January 23, 2009 Share Posted January 23, 2009 I know I was making a joke. A very bad joke obviously. Booooooooo :-\ Link to comment https://forums.phpfreaks.com/topic/142129-comparison-of-1-variable-to-multiple-values/#findComment-744458 Share on other sites More sharing options...
phpdolan Posted January 23, 2009 Author Share Posted January 23, 2009 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 Link to comment https://forums.phpfreaks.com/topic/142129-comparison-of-1-variable-to-multiple-values/#findComment-744524 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.