开发者

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:

  1. add more elseif
  2. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜