Looping

Quoting literally: Note that in PHP the switch statement is considered a looping structure for the purposes of continue.

Comments

I never understood why people

I never understood why people use the "switch" statement just to "break" at the end of each code block - see many implementations of Drupal's hook_block() or hook_nodeapi() which switch on $op. If you're not going to be making use of the fallthrough functionality switch allows, why not just use the "if/elseif" statements and avoid switch's stupid syntax?

Switches are easier to type

Switches are easier to type and read, and get optimized to jumptables in most language

$op will getting killed for

$op will getting killed for the next version of drupal.

why switch?

because it is less verbose than if/elseif :-)

Re: I never understood why people

I never understood why people use lots of if/elseif blocks to test the value of the same variable in each block ;-)

Bingo!

Bingo!

switch

with all these breaks the switch is sadly even more verbose than if/elseif blocks. Not to mention bugs if you forget breaks.
ideally switch should something be like,

switch ($foo) {
1: {
print 1;
},
2: {
print 2;
},
}

looks like hash, doesn't it?

with new closures that would be

$switch = array(
1 => function () {
print 1;
},
2 => function () {
print 2;
},
);

$switch[$i]->(); # or something like that

Anyone else use this idiom ?

<?php
   
switch(true) {
   
$a === $b =>
        echo
'a === b';
        break;
   
$a == $b =>
        echo
'a == b';
        break;
   
$a < $b =>
        echo
'a < b';
        break;
   
$a > $b =>
        echo
'a > b';
        break;
    }
?>

It is great to have the

It is great to have the opportunity to read a good quality article with useful information on topics that plenty are interested on
NS0-154 exam
642-072 exam
70-237 exam

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options