Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 468527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:41:26+00:00 2026-05-12T23:41:26+00:00

I recently found a situation in which I had two related variables that had

  • 0

I recently found a situation in which I had two related variables that had several permutations upon which I wanted to do an action if the two variables were one of half a dozen permutations.

As an example of what the data range the variables were:

$months = array('January', 'February', 'March', 'April', 'May', 'June', 'July',
    'August', 'September', 'October', 'November', 'December');
$days = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');

$month = $months[array_rand($months)];
$day = $days[array_rand($days)];
$week = rand(1, 4);

What I came up with involved a simple switch comparison of an array, like this:

function checkInputForCriteria($month, $day, $week)
{
    switch (array($month, $day))
    {
        case array('January', 'Wednesday'):
        case array('February', 'Monday'):
        case array('April', 'Tuesday'):
        case array('July', 'Saturday'):
        case array('September', 'Thursday'):
        case array('September', 'Tuesday'):
            return (in_array($week, array(1, 2)));
    }

    return FALSE;
}

Commonly this would be written as:

function checkInputForCriteria($month, $day, $week)
{
    if (
        ($month == 'January' && $day == 'Wednesday')
        || ($month == 'February' && $day == 'Monday')
        || ($month == 'April' && $day == 'Tuesday')
        || ($month == 'July' && $day == 'Saturday')
        || ($month == 'September' && $day == 'Thursday')
        || ($month == 'September' && $day == 'Tuesday')
    )
    {
        return ($week == 1 || $week == 2);
    }

    return FALSE;
}

While I realize the switch-case-array method isn’t commonly used, I don’t see any coding standard reason not to use it. PHP has no problem comparing the data in each element of the array exactly as it looks. It is compact with direct order relation between the value array elements and each comparison array element. The only reason I can see not to use this method is if it were CPU or memory intensive (which I haven’t tested).

Based on just the coding standard implications, would you accept this usage in your own code? If you saw this in someone elses code just browsing past it would you have any trouble understanding what was happening?

If this type of structure is disallowed simply because it’s not what we normally see/expect, what would be the way to introduce a structure to compare data like necessary in the above examples? Are basic if statements the only acceptable way to compare data to make logic decisions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:41:27+00:00Added an answer on May 12, 2026 at 11:41 pm

    In a language like PHP—which is largely interpreted at runtime—I think it’s fine. Better than “fine” actually: the array switch is concise and to the point.

    I think the reason such things aren’t often seen is that languages like C++ or Java don’t support them, so thought patterns which explore such solutions aren’t really well developed. Certainly the if form will optimize better in C++, even if an overloaded method of writing approximately that in C++ were created.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.