Simplify IF instructions in PHP

Asked

Viewed 201 times

5

Staff I am creating a PHP language raffle system here an excerpt of the code I started programming but the problem is that there are 19 different combinations This down here is just the first! there is a more practical way to do without using so many IF instruction 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, are 19 combinations of the variables $D1 up to $D15, but apparently the friend solved the question, so it is enough for if( $D1==$D7 && $D7==$D8 && $D8==$D9 && $D9==$D5 ) echo "gained 12 points"; for each of the 19 combinations

  • 4

    It would be good to put the whole logic in the question, as it can have a lot of different ways of solving the problem. If it were just these variables, it would be enough if( $d1==$d7 && $d7==$d8 && $d8==$d9 && $d9==$d5 ) to see if the numbers are equal; I imagine that it is not just these 5. Click [Edit] and put the game rules in the question, to have the chance to get an answer.

  • 3

    Just don’t forget that "=" is attribution. Comparison is "==".

  • 3

    Remember that = is assignment and == is comparison. Beware!

  • Thanks for the help Bacco, Marcos Regis and Mastria

  • 1

    it is possible to reduce using techniques with mathematical equations. but it depends on how the data is modeled.

  • Can start with $d1 == 1 or $d1 == 2 depending on the logic of points for it to stay if($d1==1 && $d1==$d7 && $d7==$d8 && $d8==$d9 && $d9==$d5 ), as the way you edited independent of the number will always give 12 points if they are equal.

  • The logic is that all numbers have to be equal?

  • yes the logic is that all numbers have to be equal, the numbers will be 1, 2 and 3, and the variables $D1 up to $D15, are 19 combinations where the sorter will earn 12 points, this ( $D1==$D7 && $D7==$D8 && $D8==$D9 && $D9==$D5 ) is one of them

Show 3 more comments

4 answers

5

Another way to make 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 equal to 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 little more and define an array with templates and messages/rewards and make a comparison inside the foreach, once found a positive result o break exits 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

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 is quite important to explain your punctuation logic, so we can help with various comparison methods.

Hug

  • already helped a lot but I will try to give more details are 19 combinations between $D1 until $D15, this one ( $D1==$D7 && $D7==$D8 && $D8==$D9 && $D9==$D5 ) is one of them, so just for an if for each of these combinations, this is what I will do I am grateful to everyone for the help

  • is an HTML5 slot where PHP will pass the variables through the POST and GET methods

1

I believe that the most readable and performative solution to the problem would be to convert the variables into a matrix, and to check whether 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 greater performance gain by changing function array_unique for array_flip, being like this:

$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';
    }
}

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

References

-1

Every comparison requires an IF | ELSEIF | SWITCH | $ ? (ternary operator). You can summarize your comparisons into variables, so that the IF does not get too extensive, example:

$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;
}

Even this is an advisable practice for extensive comparisons.

  • the correct would be if( $D1==$D7 && $D7==$D8 && $D8==$D9 && $D9==$D5 ) as Bacco suggested, because this is only one of the 19 conditions, where you earn the 12 points

  • This was just an example to show how to resume an IF condition, you will have to make the condition. This is a highly recommended practice, search for google. Still gain -1, sad.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.