user15129338
user15129338

Reputation:

Check day and do something if it is a workday or weekend

I want to to do something when the day is a workday, and do something else when it is a weekend.

But my function echoes "yes" every time, and I don't get it.

var_dump($dag_datumloop);// Saturday is value
if ($dag_datumloop == "Monday" or "Tuesday" or "Wednesday" or "Thursday" or 
"Friday"){
       echo "yes"; 
}else if($dag_datumloop == "Saturday" or "Sunday"){
       echo "no";
}

Upvotes: 0

Views: 79

Answers (3)

biesior
biesior

Reputation: 55798

Just use

if (in_array($dag_datumloop, ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'])){
    echo 'yes';
} else {
    echo 'no';
}

you can also use the condition in other way, like:

$isWorkDay = in_array($dag_datumloop, ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];

or

echo(in_array($dag_datumloop, ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']) ? 'yes' : 'no';

Upvotes: 0

Luka
Luka

Reputation: 69

You need to update your if statement like this:

<?php 
var_dump($dag_datumloop);// Saturday is value
if ($dag_datumloop == "Monday" or $dag_datumloop == "Tuesday" or $dag_datumloop == "Wednesday" or $dag_datumloop == "Thursday" or $dag_datumloop == "Friday"){
    echo "yes"; 
}else if($dag_datumloop == "Saturday" or $dag_datumloop == "Sunday"){
    echo "no";
}
?>

Upvotes: 0

aynber
aynber

Reputation: 23034

Your current check is only checking to see if $dag_datumloop is equal to Monday. The rest are being evaluated on their own.

if (($dag_datumloop == "Monday") or ("Tuesday") or ("Wednesday") or ("Thursday") or ("Friday")){

The other days would be evaluated as true because they're all string values. The easiest way to check it would to be use in_array instead of doing $dag_datumloop == for each one:

if(in_array($dag_datumloop, ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']))

Upvotes: 3

Related Questions