Correct use of "goto" with " if else"

I have a doubt about the correct use of "goto", I made a small example that illustrates my doubt:

<?php
    $valor01=10;
    $valor02=8;
    if($valor01 > $valor02)
    {
        echo "valor01 é maior que valor02<br>";
    }
    else
    {
        goto rotulo;
    }

    rotulo:
    {
        echo "valor02 é maior que valor01";
    }
?>

How to make the label run only if the condition of if is not satisfied?

As it stands the result is as follows:

valor01 é maior que valor02
valor02 é maior que valor01

Is this possible?

Author: Maniero, 2019-01-11

1 answers

Yes, there is, and much simpler:

<?php
    $valor01 = 10;
    $valor02 = 8;
    if ($valor01 > $valor02) echo "valor01 é maior que valor02<br>";
    else echo "valor02 é maior que valor01";
?>

See working on ideone. E no repl.it. also I put on GitHub for future reference .

The correct use of goto is to avoid it whenever it does not bring clear advantages and its example it only brings disadvantages. And it even seems to me that it is looking for a way to use a goto and not wanting to solve a problem. Why complicate what is simple?

Related: why is the use of GOTO considered bad?.

 3
Author: Maniero, 2020-08-26 13:44:18