Skip to content

Instantly share code, notes, and snippets.

@jeanpimentel
Created March 29, 2012 03:26
Show Gist options
  • Save jeanpimentel/2232964 to your computer and use it in GitHub Desktop.
Save jeanpimentel/2232964 to your computer and use it in GitHub Desktop.
PHP Grammar - Switch
unticked_statement:
...
| T_SWITCH '(' expr ')' { zend_do_switch_cond(&$3 TSRMLS_CC); } switch_case_list { zend_do_switch_end(&$6 TSRMLS_CC); }
| ...
;
switch_case_list:
'{' case_list '}' { $$ = $2; }
| '{' ';' case_list '}' { $$ = $3; }
| ':' case_list T_ENDSWITCH ';' { $$ = $2; }
| ':' ';' case_list T_ENDSWITCH ';' { $$ = $3; }
;
case_list:
/* empty */ { $$.op_type = IS_UNUSED; }
| case_list T_CASE expr case_separator { zend_do_extended_info(TSRMLS_C); zend_do_case_before_statement(&$1, &$2, &$3 TSRMLS_CC); } inner_statement_list { zend_do_case_after_statement(&$$, &$2 TSRMLS_CC); $$.op_type = IS_CONST; }
| case_list T_DEFAULT case_separator { zend_do_extended_info(TSRMLS_C); zend_do_default_before_statement(&$1, &$2 TSRMLS_CC); } inner_statement_list { zend_do_case_after_statement(&$$, &$2 TSRMLS_CC); $$.op_type = IS_CONST; }
;
case_separator:
':'
| ';'
;

Question

Why does php have the rules on line 9 and 11 (with semicolon in the beginning) in switch_case_list?


Examples:

<?php
$foo = 'a';
switch($foo) {
    ;
    case 'a': echo '1'; break;
    case 'b': echo '2'; break;
    default: echo '0'; break;
}
?>
<?php
$foo = 'a';
switch($foo):
    ;
    case 'a': echo '1'; break;
    case 'b': echo '2'; break;
    default: echo '0'; break;
endswitch;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment