0

VS Code detect errors on my php code when I try to use a reference in a foreach. This should be possible refering to PHP doc : https://www.php.net/manual/en/control-structures.foreach.php

$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}

My code is exactly the same :

$ret = [1,2,3,4] ;
foreach ( $ret as &$v )
     $v = $v * 2 ;

but VS Code still show me errors on it, and I don't know how to remove these warnings (code is working)

Any idea why ? Thanks a lot for your help !

enter image description here

enter image description here

enter image description here

enter image description here

PS : i know this code is stupid, it's just an example.

Pierre Granger
  • 1,993
  • 2
  • 15
  • 21
  • 1
    Hmm it does not do that for me, although I am using `PHP Tools` I recently replaced `intelephence` for no obvious reason – RiggsFolly Mar 11 '22 at 15:53
  • Well it looks like i'm having problems with the & char, I have an error on `error_reporting(E_ALL & ~E_NOTICE) ;` too... Unexpected 'Unknown' php I tried to disable Intelephense and add PHP Tools and PHP Debug, no change for this but it seems to be better anyway, thanks ! – Pierre Granger Mar 11 '22 at 16:07
  • 1
    I'm using Intelephense but can't reproduce that error when copy/pasting that snippet – M. Eriksson Mar 11 '22 at 16:07
  • Yes i'm starting to think it's probably not because of Intelephense... maybe something in the VSCode config but it's gonna by hard to find :| – Pierre Granger Mar 11 '22 at 16:09
  • Check on other Extensions that might be getting in the way – RiggsFolly Mar 11 '22 at 16:10
  • Also remember, this is not PHP checking your code. I assume it still runs, so it is something checking your syntax that is not up to the task – RiggsFolly Mar 11 '22 at 16:11
  • Out of interest, what version of PHP are you using – RiggsFolly Mar 11 '22 at 16:12
  • I'm also guessing that the error persists after you've saved the file? From time to time, it does tell me that I have some error even after I've fixed it, and doesn't disappear until I save the file. – M. Eriksson Mar 11 '22 at 16:13
  • @RiggsFolly php 8.1 – Pierre Granger Mar 11 '22 at 16:15
  • @M.Eriksson yes i have this one since 2 days, even after a reboot :( – Pierre Granger Mar 11 '22 at 16:16
  • Note that using references in foreach loops can have some undesirable side effects: c.f. https://www.toptal.com/php/10-most-common-mistakes-php-programmers-make – schmauch Mar 17 '22 at 15:09

1 Answers1

0

That was not a problem Intelephense, but with PHP IntelliSense : https://github.com/felixfbecker/vscode-php-intellisense

Disabling it remove the errors i had. I did not post an issue on intellisense Github.

Pierre Granger
  • 1,993
  • 2
  • 15
  • 21