views:

78

answers:

3

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)

+4  A: 

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",
         ...
       );
Yada
im level 3/10 on php. can you give me some more information about using associative arrays?
Ronnie Chester Lynwood
Associative arrays are just arrays with text as keys instead of numbers. http://php.net/manual/en/language.types.array.php So you can use $types['movies'] to get "DVDRip, DVDScr"
Yada
*cough*Google*cough*
Atli
did you just copy paste the other 2 answers in your own? jesus! http://stackoverflow.com/posts/2274630/revisions
cherouvim
+1  A: 

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.

Matthew Flaschen
thank you for your answer. to learn hard things, i will try to use associative arrays as Yada said. :)
Ronnie Chester Lynwood
+1  A: 

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.

cherouvim