Correct use of "goto" with "if Else"

Asked

Viewed 201 times

0

I have a question about the correct use of the "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 ensure that the label is only implemented if the if don’t be satisfied?

As it stands the result is as follows:

valor01 é maior que valor02
valor02 é maior que valor01

That’s possible?

  • The goto is not recommended, as it is difficult to understand when the project gets larger.

  • And just put the code right into Isis, no drip. That’s what Isis is for...

  • In this example of yours there is no sense to use goto, just make the logic inside the else, in fact, I don’t think I ever need to use this command, since it creates a spaghetti code.

  • Fully possible is just to do a table test that you will discover the snippets of your code that will be executed. The goto command exists for historical reasons. It was widely used in the programming style of the 1960s but after that its use was execrated by all the problems it causes. In your case just put another drop after the if skipping the section under label.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer that you find useful on the entire site (so accepting you will gain privilege to vote).

  • If it is still necessary, OOP, do not use the drop, create a function and call it on Else. We have evolved.. hehe

Show 2 more comments

1 answer

3

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";
?>

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

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

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

  • Maniero is not about trying to "reinvent the wheel", or it is "a way to use goto and not wanting to solve a problem..." As I said, I’m relearning, I saw the goto option and I want to understand more about it,I believe that if it was included in PHP it must have some purpose. The article you recommended I had already read, but I believe it is always good to hear more opinions about it. The example I quoted is just to illustrate a hypothesis, because although I am not an expert in PHP I also did not understand the purpose of using the function. But anyway, thanks for the return.

Browser other questions tagged

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