开发者

Can I use IF multiple times to switch between multiple options in PHP?

开发者 https://www.devze.com 2022-12-20 20:06 出处:网络
hey i got 9 type on my web. i have to set different keywords each type. with this script; if ($type = movie) {

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

精彩评论

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

关注公众号