Can I use IF multiple times to switch between multiple options in PHP?
hey i got 9 type on my web. i have to set different keywords each type. with this script;
if ($type = movie) {
$yazdir = "DVDRip, DVDScr";
}
elseif ($type = game)开发者_如何转开发 {
$yazdir = "Full Version, Patch";
}
i can write keywords for two type. how to repeat this correctly for other types? (echo paramether must be $yazdir)
Three options:
- add more elseif
- You can use
switch
http://php.net/manual/en/control-structures.switch.php
3.use associative arrays
$types = array(
"movies" => "DVDRip, DVDScr",
"games" => "Full Version, Patch",
...
);
Just keep adding elseif
.
if ($type == "movie") {
$yazdir = "DVDRip, DVDScr";
} elseif ($type == "game") {
$yazdir = "Full Version, Patch";
} elseif ($type == "book") {
$yazdir = "Warez book";
}
Or you can use a switch, as Yada said. Note that you must use break, or it will fall through.
If you find yourself doing that many many times, then the problem at hand is best solved by an associative array which in your case will map $type keys to $yazdir values.
精彩评论