note 79394 added to language.control-structures
| From: | MauriceR at osu1 dot php dot net | Date: | Sun, 25 Nov 2007 00:15:01 +0000 |
| Subject: | note 79394 added to language.control-structures | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-132547@lists.php.net to get a copy of this message | ||
In reply to fteng1 at gmail dot com (04-Aug-2007 06:48):
That's a horrible (and probably really slow) way to do that. Never use that! What you need to
use for this is the switch control operator.
<?php
switch( $year ) {
case 1948:
case 1960:
case 1972:
case 1984:
case 1996:
case 2008:
echo "Rat";
break;
case 1949:
case 1961:
case 1973:
case 1985:
case 1997:
case 2009:
echo "Ox";
break;
// etc...
}
?>
In your case PHP would first have to make arrays of the years (which means storing all the values in
it's memory) before checking each of these.
The switch control structure was specifically made for checks like these. For simpicity's and
speed's sake.
----
Server IP: 69.147.83.197
Probable Submitter: 217.122.53.206
----
Manual Page -- http://www.php.net/manual/en/language.control-structures.php
Edit -- https://master.php.net/note/edit/79394
Del: integrated -- https://master.php.net/note/delete/79394/integrated
Del: useless -- https://master.php.net/note/delete/79394/useless
Del: bad code -- https://master.php.net/note/delete/79394/bad+code
Del: spam -- https://master.php.net/note/delete/79394/spam
Del: non-english -- https://master.php.net/note/delete/79394/non-english
Del: in docs -- https://master.php.net/note/delete/79394/in+docs
Del: other reasons-- https://master.php.net/note/delete/79394
Reject -- https://master.php.net/note/reject/79394
Search -- https://master.php.net/manage/user-notes.php