note 35778 deleted from control-structures.switch by crell
| From: | crell@php.net | Date: | Wed, 29 Jun 2022 17:51:10 +0000 |
| Subject: | note 35778 deleted from control-structures.switch by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218660@lists.php.net to get a copy of this message | ||
Note Submitter: havar at henriksen dot nu
----
Remember, that you also could use functions in a switch.
For example, if you need to use regular expressions in a switch:
<?php
$browserName = 'mozilla';
switch ($browserName) {
case 'opera':
echo 'opera';
break;
case (preg_match("/Mozilla( Firebird)?|phoenix/i",
$browserName)?$browserName:!$browserName):
echo "Mozilla or Mozilla Firebird";
break;
case 'konqueror':
echo 'Konqueror';
break;
default:
echo 'Default';
break;
}
?>
or you could just use a regular expression for everything:
<?php
$uri = 'http://www.example.com';
switch (true) {
case preg_match("/$http(s)?/i", $uri, $matches):
echo $uri . ' is an http/https uri...';
break;
case preg_match("/$ftp(s)?/i", $uri, $matches):
echo $uri . ' is an ftp/ftps uri...';
break;
default:
echo 'default';
break;
}
?>