Odd behaviour in a switch statement

前端 未结 2 1119
渐次进展
渐次进展 2020-12-07 01:43

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 the

2条回答
  •  悲哀的现实
    2020-12-07 02:21

    The key point is that a switch() statement performs comparisons between the parameter and the labels. That means that you have to deal with PHP comparison and type casting rules. Just see some examples:

    The reference can be found at:

    • http://es.php.net/manual/en/language.operators.comparison.php
    • http://es.php.net/manual/en/types.comparisons.php#types.comparisions-loose
    • http://es.php.net/manual/en/control-structures.switch.php

    Initializing an unset GET parameter to 0 is a pretty strange design decision. You should deal with that particular case in a separate fashion to make it clear that it's a special situation:

    if( $name===0 ){
        return '';
    }
    switch($name){
        // ...
    }
    

提交回复
热议问题