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

后端 未结 3 1617
孤街浪徒
孤街浪徒 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:10

    Use break to prevent follow through to next case:

    switch($k)
    {
    case 'pg_id':
    case 'pg_tag':
    case 'pg_user':
      // any match triggers this block; break causes no-op
      break;
    default:
      $result = $connection->run_query($sql,array(...));
    }
    

    I'm not sure why you want to use a switch statement for this though.

    If it's for readability, you could try this instead:

    if($k != 'pg_id' &&
       $k != 'pg_tag' &&
       $k != 'pg_user')
    {
      $result = $connection->run_query($sql,array(...));
    }
    

提交回复
热议问题