Is it correct to create a variable within an if?

Asked

Viewed 619 times

4

it is correct to create a variable within an if ?

In the code below you create a variable within the if, and avoid using the same filter twice.

if($id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT)):
    /* Código */
endif;

What do you think of the practice within the IF? That’s against some PSR rule (PHP Standards Recommendation) ?

{Thank you}

  • What would be the version of this code without the if? sometimes is last other than xD

  • 1

    I think it is good to put the question of PSR in the title, because the rest merely depends on opinion. If you are going to use the information, the strange thing would be to just not create the variable. Although in the case of if you could create before.

  • 1

    If it wasn’t for the poor people who use while($busca=mysql_fetch_array($sql))

  • I believe it is valid, but to certain things you can use the && or || before creating two conditions in sequence, but goes from the solution to solving....

  • 1

    There’s no problem, it goes from the need to do this.

1 answer

5


First of all, the if is not a good example, because you can "create" the variable only once, even outside:

$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT)
if( $id ):
   /* Código que usa o $id, sem "criar" de novo */
endif;

But in a while already complicates, so I’ll use this as an example:

while ($row = $result->fetch_row()) {
    printf ("%s (%s)\n", $row[0], $row[1]);
}

In this case, not only is it correct, but it is a example taken from the PHP manual.

Since in the while not only is it a common case, but often necessary, I see no reason not to use in the if. In the end, readability is always the most important thing. And for that, it only depends on the context where you will use it. In the above example there was certainly no gain in separating the lines.

If you have any PSR saying otherwise, you have to fix the PSR instead of complicating the code.

  • I think in this case(if) still fits a naughty suit ;)

  • @rray but the premise is he use the $id inside the block if after right? If I put a ternary there, it complicates reuse, if it is in more than one place.

  • Yes, it can be create the variable, but in a case the variable will only exist within if that option is not valid. The if in this way saves me from having to create the variable inside the if, because it has already been created. My real doubt is about this one somehow wrong, as the Netbeans IDE generates an alert stating that it was accidental to create the variable within the if!

  • 2

    "in a case where the variable will only exist within if this option is not valid" This is not true, see http://ideone.com/cvzNmW - if created within the IF test, it is of the external scope. Different from creating inside the if BLOCK.

  • @Bacco I said wrong, I didn’t mean she wouldn’t run inside the if, I meant it doesn’t make sense to create her out of the if. Sorry I made a mistake when it came to expressing myself.

  • 1

    @jjdomroam Anyway, it’s good to remember the scope :) We’re looking at a simple example, if it’s a complex if where you create the variable and do operations with it, it makes sense yes. Imagine that: if( ( $id = MegaCalculo() * 45.2 / Sqrt( 999999999) ) < AjusteSenoidalDoFatorX( 2347) ) In this case, it becomes interesting to take the $id = MegaCalculo() * 45.2 / Sqrt( 999999999 ) and leave alone if( $id < AjusteSenoidalDoFatorX( 2347 ) ), no? As I said, the context is in charge. There are times that are better one way, there are times that are better another way. It goes from the complexity of expressions.

  • @Bacco Yes, it makes sense in that case to create the variable out of if, even in your first example it makes sense to take it out of if if I’m going to use it elsewhere out of it. My own doubt is because of my Netbeans IDE that generates a small warning that it was accidental to create the variable, which gives me the idea that I would be doing something wrong!

  • @jjdomroam try to use ( ) to make it clear to IDE that you did it on purpose. For example: if( ( $id = Funcao() ) ): - I don’t know if it works in netbeans, but it might. Although I don’t know which is worse, the alert or the ( ) disturbing reading.

  • @Bacco worked perfectly :v, my IDE does not generate more alert on line. Thanks for the tip!

  • @Bacco It does not disturb much the reading, now I am in doubt even if it is worth just include the () for the whim of the IDE.

  • @jjdomroam complicated decision :) See if you can’t disable this specific detection in the options. I’m not familiar with netbeans

  • @Bacco has, yes, I’ve deactivated, thank you!

Show 7 more comments

Browser other questions tagged

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