Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing switch to compare multiple values in an array in PHP
    primarykey
    data
    text
    <p>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.</p> <p>As an example of what the data range the variables were:</p> <pre><code>$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); </code></pre> <p>What I came up with involved a simple switch comparison of an array, like this:</p> <pre><code>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; } </code></pre> <p>Commonly this would be written as:</p> <pre><code>function checkInputForCriteria($month, $day, $week) { if ( ($month == 'January' &amp;&amp; $day == 'Wednesday') || ($month == 'February' &amp;&amp; $day == 'Monday') || ($month == 'April' &amp;&amp; $day == 'Tuesday') || ($month == 'July' &amp;&amp; $day == 'Saturday') || ($month == 'September' &amp;&amp; $day == 'Thursday') || ($month == 'September' &amp;&amp; $day == 'Tuesday') ) { return ($week == 1 || $week == 2); } return FALSE; } </code></pre> <p>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).</p> <p>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?</p> <p>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 <code>if</code> statements the only acceptable way to compare data to make logic decisions?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload