You can simplify the IF statements in PHP

5

Personal I'm creating a draw system in PHP language Here is an excerpt of the code I started to program but the problem is that there are 19 different hit combinations this one down here is just the first! is there a more practical way to do it without having to use as many IF statement lines?

<?php
    ...................
    //primeira combinação de acertos
    if (($d1=3) && ($d7=3) && ($d8=3) && ($d9=3) && ($d5=3))
    echo "ganhou 12 pontos";
    if (($d1=2) && ($d7=2) && ($d8=2) && ($d9=2) && ($d5=2))
    echo "ganhou 12 pontos";
    if (($d1=1) && ($d7=1) && ($d8=1) && ($d9=1) && ($d5=1))
    echo "ganhou 12 pontos";
?>

complementing, there are 19 combinations of the variables $ d1 to $ d15, but apparently the friend solved the question, ($ d1 == $ d7 & d7 == $ d8 & & d8 == $ d9 & & d9 == $ d5)   echo "earned 12 points"; for each of the 19 combinations

    
asked by anonymous 11.11.2015 / 16:24

4 answers

5

Another way to do these comparisons is to transform the variables into an array and create another array as a template, so compare if the data sent by the user is the same as the information of some template.

<?php

$d1 = 2;
$d7 = 2;
$d8 = 2;
$d9 = 2;
$d5 = 2;

$gabarito['1'] = array_fill(0, 5, 3);
$gabarito['2'] = array_fill(0, 5, 2);
$gabarito['3'] = array_fill(0, 5, 1);

$respondido = array($d1, $d7, $d8, $d9, $d5);

if($respondido === $gabarito[1]){
    echo 'Ganhou 8 pontos';
}else if($respondido === $gabarito[2]){
    echo 'Ganhou 12 pontos/gabarito 2';
}else if($respondido === $gabarito[3]){
    echo 'Ganhou 12 pontos/gabarito 3';
}else{
    echo 'Mais sorte na próxima';
}

You can increment a bit more and define an array with the templates and the messages / rewards and make a comparison inside the foreach, once found a positive result the break leaves the loop and displays the message.

$gabaritos = array(array('msg' => 'Ganhou 8 pontos', 'respostas' => array_fill(0, 5, 3)),
                   array('msg' => 'Ganhou 12 pontos/ gabarito2', 'respostas' => array_fill(0, 5, 2)),
                   array('msg' => 'Ganhou 12 pontos/ gabarito3', 'respostas' => array_fill(0, 5, 3))                        
            );  


$respondido = array($d1, $d7, $d8, $d9, $d5);


foreach($gabaritos as $gabarito){
    if($respondido === $gabarito['respostas']){
        $msg = $gabarito['msg'];
        break;
    }else{
        $msg = 'Perdeu playboy';
    }
}

echo $msg;

Reference:

Check if two arrays are equal

    
11.11.2015 / 17:25
1

Another way to reduce your comparison is:

if($d1.$d7.$d8.$d9.$d5 == 11111) echo 'ganhou 12 pontos';

Or more precise:

if($d1.$d7.$d8.$d9.$d5 === '11111') echo 'ganhou 12 pontos';

It's very important to explain your punctuation logic, so we can help with various methods of comparison.

Embrace

    
11.11.2015 / 16:59
1

I think the most readable and performative solution to the problem would be to convert the variables into an array, and to check that each combination has only one value.

$combinacoes = array(
    array($d1,$d5,$d7,$d8,$d9),
    array($d2,$d3,$d4,$d8,$d9),
    array($d3,$d4,$d5,$d6,$d7),
    array($d5,$d6,$d7,$d8,$d9),
    array($d4,$d5,$d6,$d7,$d8)
);

foreach ($combinacoes as $combinacao) {
    if(count(array_unique($combinacao)) === 1) {
        echo 'ganhou 12 pontos';
    }
}

It is possible to have an even higher performance gain by switching the function array_unique to array_flip , thus:

$combinacoes = array(
    array($d1,$d5,$d7,$d8,$d9),
    array($d2,$d3,$d4,$d8,$d9),
    array($d3,$d4,$d5,$d6,$d7),
    array($d5,$d6,$d7,$d8,$d9),
    array($d4,$d5,$d6,$d7,$d8)
);

foreach ($combinacoes as $combinacao) {
    if(count(array_flip($combinacao)) === 1) {
        echo 'ganhou 12 pontos';
    }
}

In this way, if you need to change or add combinations in the future, it will be much easier.

References

12.11.2015 / 08:26
-1

Every comparison requires an IF | ELSEIF | SWITCH | ? : (ternary operator). You can summarize your comparisons in variables, so that the IF is not too long, eg:

$8pontos = ($d1 == 3 && $d7 == 3 && $d8 == 3 && $d9 == 3 && $d5 ==3) ? true : false;
$12pontos = ($d1 == 1 && $d7 == 1 && $d8 == 1 && $d9 == 5 && $d5 == 7) ? true : false;

$pontos = 0;

if ($8pontos) {
    $pontos = 8;
} elseif ($12pontos) {
    $pontos = 12;
}

Including this is an advisable practice for extensive comparisons.

    
11.11.2015 / 16:47