RE: [PHP] Another problem with conditional statements
| From: | Ford, Mike [LSS] | Date: | Fri, 20 Dec 2002 11:19:23 +0000 |
| Subject: | RE: [PHP] Another problem with conditional statements | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-128972@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Beauford.2002 [mailto:beauford.2002@rogers.com]
> Sent: 20 December 2002 03:15
>
>
> Using switch would be more efficiant as it would stop once a
> match is made
> (if you use break), but with eleif statements each one is evaluated in
> order.
Not sure that's true -- a sequence of elseifs will also stop evaluating once one of the
conditions has evaluated to true. The essential difference is that a switch/case structure is a
repeated test against a single value (possibly derived from an expression), whereas an if ... elseif
sequence can test multiple completely disjoint conditions.
> $chr = substr($a,$i,1);
> switch (TRUE) {
>
> case $chr == "á" || $chr == "à" || $chr == "ã" || $chr ==
> "â":
> $a = str_replace(substr($a,$i,1),"a",$a);
> break;
>
> case $chr == "é" || $chr == "è" || $chr == "ê":
> $a = str_replace(substr($a,$i,1),"e",$a);
> break;
>
> }
Actually, I'm not sure that's a good example, either! I'd have thought it was better
written as:
switch (substr($a,$i,1)) {
case "á":
case "à":
case "ã":
case "â":
$a = str_replace(substr($a,$i,1),"a",$a);
break;
case "é":
case "è":
case "ê":
$a = str_replace(substr($a,$i,1),"e",$a);
break;
}
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211