note 35778 modified in control-structures.switch by vrana
| From: | vrana@php.net | Date: | Tue, 17 Aug 2004 13:54:28 +0000 |
| Subject: | note 35778 modified in control-structures.switch by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-79994@lists.php.net to get a copy of this message | ||
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;
}
?>
--was--
Remember, that you also could use functions in a switch.
For example, if you need to use regular expressions in a switch:
$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:
$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;
}
http://php.net/manual/en/control-structures.switch.php