dimanche 2 août 2015

How to condense multiple IF statements simply in PHP

Here if my code. It works and runs and does everything I want it to do, but its too long. How can I condense it? My PHP level is basic.

Would I use an array? or a function? Can you kindly post the code you would use to make this more efficient?

<?php
$product = $result[CategoryId];

if ($product == "1") {
    echo "Tonneau Cover to suit";
} elseif ($product == "2") {
    echo "Clip On Tonneau Cover to suit";
} elseif ($product == "3") {
    echo "INSERT";
} elseif ($product == "4") {
    echo "INSERT";
} elseif ($product == "5") {
    echo "INSERT";
} elseif ($product == "6") {
    echo "INSERT";
} elseif ($product == "7") {
    echo "INSERT";
} elseif ($product == "8") {
    echo "INSERT";
} elseif ($product == "9") {
    echo "INSERT";
} elseif ($product == "10") {
    echo "INSERT";
} elseif ($product == "11") {
    echo "INSERT";
} elseif ($product == "12") {
    echo "INSERT";
} elseif ($product == "13") {
    echo "INSERT";
} elseif ($product == "14") {
    echo "INSERT";
} elseif ($product == "15") {
    echo "INSERT";
} elseif ($product == "16") {
    echo "INSERT";
} elseif ($product == "17") {
    echo "INSERT";
} else {
    echo ".";
}
?>

Aucun commentaire:

Enregistrer un commentaire