vendredi 9 août 2019

Is there a way to reduce if statments in my code?

i'm having this problem with if statments, so my main goal is to get option value after submition then after having my values ijust use if statments to return some results

i did the math and it's seems that i'm having about 216 if statments is there any solution for that ? please help thanks

<?php

$var1 = $_POST['option1'];
$var2 = $_POST['option2'];
$var3 = $_POST['option3'];
$var4 = $_POST['option4'];
$var5 = $_POST['option5'];
$var6 = $_POST['option6'];


if($var1 == "1" || $var2 == "1" || $var3 == "1" || $var4 == "1" || $var5 == "1" || $var6 == "1"){
    //do some thing
}
if($var1 == "2" || $var2 == "1" || $var3 == "1" || $var4 == "1" || $var5 == "1" || $var6 == "1"){
    //do some thing
}
if($var1 == "3" || $var2 == "1" || $var3 == "1" || $var4 == "1" || $var5 == "1" || $var6 == "1"){
    //do some thing
}
if($var1 == "4" || $var2 == "1" || $var3 == "1" || $var4 == "1" || $var5 == "1" || $var6 == "1"){
    //do some thing
}
if($var1 == "5" || $var2 == "1" || $var3 == "1" || $var4 == "1" || $var5 == "1" || $var6 == "1"){
    //do some thing
}
if($var1 == "1" || $var2 == "2" || $var3 == "1" || $var4 == "1" || $var5 == "1" || $var6 == "1"){
    //do some thing
}
if($var1 == "1" || $var2 == "3" || $var3 == "1" || $var4 == "1" || $var5 == "1" || $var6 == "1"){
    //do some thing
}
/*.....................................
....................................
...................................
................................
            an other 216 if statments
*/

//last one should be

if($var1 == "5" || $var2 == "3" || $var3 == "4" || $var4 == "5" || $var5 == "1" || $var6 == "2"){
    //do some thing
}
?>

i have herd about the ternary operator and refactoring but i dont know how to use them

Aucun commentaire:

Enregistrer un commentaire