unused local variable - bug ? (PS-130.1639)

because of the "IF", phpstorm cannot say, that it´s unused, or am i wrong here ???


Comment actions Permalink

Hi there,

Your screenshot shows not enough details to give you any good advice. Please show more (whole block/function).

Right now (based on such limited info) I guess it could be because condition in IF statement will always return true and that statement will always be executed. Or (which is more likely, if you think about it) you do have another unconditional assigment later without using this variable before that.

If you do see some unusual erros in other places -- see if "File | Invalidate Caches..." will help here.

Comment actions Permalink

here you have a piece of code with the same warnings...

<? function test() {     $a = "";     $b['test'] = 5;     $b['test2'] = 5;          if ($b['test'] == $b['test2']) {         $a = 3;     } else {         $a = 5;     } } test(); exit;

the warning will be, that $a is a unsused local variable and that´s wrong in my eyes!

Comment actions Permalink

I see nothing wrong here in this particular code example:

1) The first $a = ""; is not really necessary because new value will be assigned in ANY CASE inside if-else statement.

2) Since you do not use value of $a later (after if-else block), such assigment is also marked as unused (what is the point of having a variable and assign value to it if it never get used?).

If I add echo $a; in the end of the function, the "unused variable" marks for $a inside if-else block will disappear.

If you see such error in your REAL code -- please show it here (the text, same as you did above; not a screenshot of it).

Comment actions Permalink

okay, my fault... the code inspection is much smarter than me ;-)
i rechecked my code and took a deeper look into it... and finally your explanation make sense...

thank you for your help! ;)


Please sign in to leave a comment.