Tag Archives: switch-statement

Why is a `switch` considered a looping structure for the purposes of `continue`?

I just got bit by assuming the following:
foreach ($arr as $key => $value) {
  switch($key) {
    // ... some other cases
    default:
      continue;
      // ^== assumption: move on to the next iteration of the foreach
      //     actual PHP: treat
Continue reading
Tagged , , | Leave a comment

Is there a way to simplify this case statement?

I have this PHP case statement
switch ($parts[count($parts) - 1]) {
    case 'restaurant_pos':
        include($_SERVER['DOCUMENT_ROOT'] . '/pages/restaurant_pos.php');
        break;
    case 'retail_pos':
    include($_SERVER['DOCUMENT_ROOT'] . '/pages/retail_pos.php');
        break;
    .....

}
Which works great but I have many many files (like 190) and I would… Continue reading
Tagged , , , | Leave a comment

PHP allowing invalid code in switch statements

I’m having some trouble understanding why the following doesn’t result in a compiler error in 5.3.3 (errored-out correctly on my coworkers 5.2.5):
<?php
    echo "startingn";

    switch(1) {
        case 2:
            echo "twon";
            break;
        defalut:        // note the misspelling
            echo "deflautn";
    }
Continue reading
Tagged , , | Leave a comment

Odd behaviour in a switch statement

I’m writing code for a sortable table, where clicking the links in the header change the ORDER BY executed when generating a set of search results (the case where there there is no valid order by causes the query to… Continue reading
Tagged , | Leave a comment

Is it possible to use || in PHP switch?

switch ($foo)
    {
        case 3 || 5:
           bar();
        break;

        case 2:
          apple();
        break;
    }
In the above code, is the first switch statement valid? I want it to call the function bar() if the value of $foo is either… Continue reading
Tagged , , | 3 Comments

How to use a switch case 'or' in PHP?

is there such thing (in PHP anyway) for an OR operator in a switch case? something like..
switch ($value) {

case 1 || 2:
echo 'the value is either 1 or 2';
break;

}
Incoming search terms:como usar switch… Continue reading
Tagged , , | 2 Comments

Best way to do a PHP switch with multiple values ver case?

How would you do this PHP switch statement? Also note that these are much smaller versions, the 1 I need to create will have a lot more values added to it. Version 1:
switch ($p) {
    case 'home':
    case '':
Continue reading
Tagged , | 1 Comment

Break out out forloop but within switch statement php

When I normally want to break out of a foreach loop before all of the iterations have completed I simply use a break; statement. e.g.
foreach($nodelist as $node) {
   if($metCriteria) {
       break;
   }
}
But my next example has… Continue reading
Tagged , , , | 3 Comments
1 pages