This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Conditionals and Control Flow Forum

View Course » View Exercise

414 points
05b418eac3a7a0dc8cd12e3b51b692e9?s=140&d=retro
Submitted by
odyer4
over 2 years ago

Is there a way of adding multiple values to a variable?

For example:

    $WeekDays=("Monday" "Tuesday" "Wednesday" "Thursday" "Friday");

        if ($WeekDays == "Thursday") {
            echo "TGIF";
        }
        else {
                echo "Yay the weekend!";
        }

The idea of this code is to check if the day of the week is between Monday & Friday

How would you make something like this work?


1 vote

permalink

You're not looking for a single variable assignment - variables have single values - you're looking for a data structure. The way I would approach this is by putting all the weekdays in an array and checking if a specific weekday variable is in the array. For example:

$WEEKDAYS = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday');
// The WEEKDAYS variable holds all the values for days of the week.
$someDay = "Saturday"; /* Let's arbitrarily assign the value we want to check    against our  array */
if (in_array($someDay, $WEEKDAYS)) // We're checking if this day is in the week. {
        echo "TGIF";
    }
    else {
            echo "Yay the weekend!";
    }

So this if would produce "Yay the weekend!" Does that help?

161 points
92562d60c8ba3eadafb12ce301fe35a4?s=140&d=retro
Submitted by
Robomuffin
over 2 years ago