Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.