Shorthand or Dispatch switch-statement with function?

208 views Asked by At

There is any way to shorthand or dispatch this to one-line code?

$asd='http://asd.asd';
// $asd='[email protected]';
// $asd='+123123';

switch ($asd){
    case strpos($asd, 'http')===0:
        $qwe='href';
        break;
    case strpos($asd, '@')>1:
        $qwe='mailto';
        break;
    case strpos($asd, '+')===0:
        $qwe='tel';
        $break;
}
1

There are 1 answers

1
Anis Alibegić On BEST ANSWER
<?php

   $asd = 'http://asd.asd';
   // $asd = '[email protected]';
   // $asd = '+123123';

   $qwe = (strpos($asd, 'http') === 0 ? 'href' : (strpos($asd, '@') > 1 ? 'mailto' : (strpos($asd, '+') === 0 ? 'tel' : '')));

   echo $qwe;
?>

It's not easy to read but you wanted it to be a single line of code. It's called ternary operator and you read more about it here.