How to convert `if condition` to `switch` method

后端 未结 3 1620
孤街浪徒
孤街浪徒 2021-01-28 10:27

How can I write this if condition in switch method?

if( $k != \'pg_id\' && $k != \'pg_tag\' && $k != \'pg_user\' )
{
           


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-01-28 11:06

    A switch condition is likely to be faster (jump table) and a bit easier to read. You can skip the break; if the result of the condition is the same and an improved syntax is to use curly braces inside each condition:

    switch ($k)
    {
     case 'pg_id':
     case 'pg_tag':
     case 'pg_user': {
     break;
     }
     default: {
       $result = $connection->run_query($sql,array(...));
     }
    }
    

提交回复
热议问题