Reputation: 663
Is this possible?
$d=date("D");
...
else if ($d=='(Thu|Fri|Sat)') {
I can get a single day of the week working.
if ($d=='Wed') {
Thanks
Upvotes: 0
Views: 358
Reputation: 23316
$d == '(Thu|Fri|Sat)'
will only match if $d
matches that exact string.
You could:
preg_match('/^(Thu|Fri|Sat)$/', $d)
,in_array($d, array( 'Thu', 'Fri', 'Sat' ))
, orif($d == 'Thu' || $d == 'Fri' || $d == 'Sat');
Upvotes: 2
Reputation: 12613
No. For what you want, in_array()
is the best choice.
if(in_array($d, array('Thu', 'Fri', 'Sat'))
// do something if any case is true
If you want more control, switch
and case
are very handy and give you more control over different cases.
$d = date('D');
switch($d) {
case 'Thu':
case 'Fri':
case 'Sat':
// do something for Thu, Fri, or Sat
break;
case 'Mon':
// do something only for Mon
case 'Tue':
// do something for Mon or Tue.
break;
}
The first set of cases will apply the following code (up until break
) if any or all of the conditions are met.
The second set will apply the code between cases Mon
and Tue
if Mon
is true, and then continue to Tue
if it is true.
switch
and case
can be very useful, especially here if you want real control.
Upvotes: 2
Reputation: 10539
Just use in_array()
if (in_array($d, array("Thu", "Fri", "Sat"))) {
}
Upvotes: 3
Reputation: 78991
Why can't you use or
or ||
?
else if ($d=='Thu' || $d=='Fri' || $d=='Sat') {
If you dont want to stick to simplicity then use preg_replace()[docs]
preg_match('^(Thu|Fri|Sat)$', $yourtext, $matches, PREG_OFFSET_CAPTURE);
if(count($matches)) {
/// found
}
Upvotes: 2
Reputation: 2780
You could use the OR operator:
else if ($d=='Thu' || $d=='Fri' || $d=='Sat') {
Upvotes: 2
Reputation: 12843
Try
if(in_array($d, array('Thu', 'Fri', 'etc'))){
}
Or with ||
if($d == 'Thu' || $d == 'Fri' || etc)
Upvotes: 1