Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » PHP Development Tools (PDT) » "Variable is never used" in foreach loops
"Variable is never used" in foreach loops [message #1792520] Thu, 19 July 2018 02:00 Go to next message
Yurij Zagrebnoy is currently offline Yurij ZagrebnoyFriend
Messages: 2
Registered: August 2013
Junior Member
Hi,

I often use PHP foreach to iterate over array keys, like:
foreach ($list_of_keys as $key => $_) {
// ...
}


This is about 2 times faster on big arrays and doesn't consume additional memory compared to using array_keys()
foreach (array_keys($list_of_keys) as $key) {
// ...
}


The problem is that each $_ variable produces warning "Variable $_ is never used".


Is there any way to configure PHP Validator to exclude variables with certain names from checking their usage?


BTW Thank you for your great work on PDT!
Re: "Variable is never used" in foreach loops [message #1793466 is a reply to message #1792520] Wed, 08 August 2018 18:03 Go to previous messageGo to next message
Moses Lecce is currently offline Moses LecceFriend
Messages: 2
Registered: July 2009
Junior Member
I am also experiencing this issue, which basically means I have a bunch of warnings that can't really be resolved without implementing less optimized code.

In addition to this, I have code in third party files (that I do not wish to edit and correct) which come up as warnings (is there a way to exclude a file/directory from warnings, I cannot find such a setting).

Another scenario I cannot seem to handle is something where you are using a reference variable:

e.g.
$this = array();
$this2 =& $this;
$this2[] = "a";

Will tell me that $this2 is never used.
Re: "Variable is never used" in foreach loops [message #1794915 is a reply to message #1793466] Tue, 11 September 2018 05:53 Go to previous messageGo to next message
Vladimir Paramygin is currently offline Vladimir ParamyginFriend
Messages: 1
Registered: September 2018
Junior Member
Yeah, having the same problem. Upgraded to Photon yesterday and now my project is littered with warnings about undefined and unused variables :(
Re: "Variable is never used" in foreach loops [message #1796669 is a reply to message #1792520] Wed, 17 October 2018 09:42 Go to previous messageGo to next message
Fladnag Fladnag is currently offline Fladnag FladnagFriend
Messages: 1
Registered: October 2018
Junior Member
you can avoid this error with... this comment, before variable usage :

/** @var mixed $key */

But yes, Photon PDT validation is a nightmare... :
- too many false positive (output variable is undeclared, page fragment variable is undeclared, global variable is undeclared, array variable access is undeclared)
- type in comments are checked... and send ERROR if not found. You can lower or disabled it, but not for comments only.
- etc...
Re: "Variable is never used" in foreach loops [message #1796781 is a reply to message #1796669] Thu, 18 October 2018 19:30 Go to previous message
Moses Lecce is currently offline Moses LecceFriend
Messages: 2
Registered: July 2009
Junior Member
Thank you!
Previous Topic:Spell check and wysiwyg plugins
Next Topic:Find Function within PHP project
Goto Forum:
  


Current Time: Wed Nov 14 18:33:24 GMT 2018

Powered by FUDForum. Page generated in 0.01829 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top