0

I have Drupal 7 site. I have the following Switch structure.

$day = (int)$node->field_hours_count[LANGUAGE_NONE][0]['value'];
switch ($day) {
    case 1:
        constructNode($node,"sunday");
        echo 'node updated successfuly';
        break;
    case 2:
        constructNode($node,"monday");
        echo 'node updated successfuly';
        break;
    case 3:
        constructNode($node,"tuesday");
        echo 'node updated successfuly';
        break;
    default :
       echo 'no node found'; 
       exit();
   } 

Here constructNode() is a function which accepts two parameters.

function constructNode($node,$dayOfWeek)
{
   //core operation
  return $node;
}

Issue is suppose $day =1 then case 1 is executed plus default case. For every value of $day, default case is getting executed.

php - version v 5.5.12

How do I prevent this?

Kgn-web
  • 7,047
  • 24
  • 95
  • 161

1 Answers1

1

Issue is suppose $day = 1 then case 1 is executed plus default case. For every value of $day, default case is getting executed.

That's not how Switch statements work. After any case is executed, the break exits the statement so nothing else will be matched. The only possible way this is happening is if you're calling the Switch statement twice with different values.

mister martin
  • 6,197
  • 4
  • 30
  • 63