9

I have recently set up vsCode with PHP. However, it shows errors on following lines.

public static function myFunction(): bool|string

error : syntax error, unexpected '|', expecting ';' or '{'

$app->options('/{routes:.+}', fn (ResponseInterface $response) => $response);

error : syntax error, unexpected '$response' (T_VARIABLE), expecting ')'

How can I make it recognize that this is the correct syntax.

Paul T.
  • 4,703
  • 11
  • 25
  • 29
ConfusedProgrammer
  • 479
  • 1
  • 4
  • 14

2 Answers2

2

Unless you install a third-party extension that provides its own custom PHP parser, Visual Studio Code just relies in the PHP interpreter you install in order to lint files. That's provided by the PHP Language Features builtin extension.

I will pick your PHP interpreter from your PATH variable. A simple way to figure it out is to type php -v in a CMD or PowerShell window:

C:\>php -v
PHP 8.1.15 (cli) (built: Jan 31 2023 23:50:48) (ZTS Visual C++ 2019 x64)
Copyright (c) The PHP Group
Zend Engine v4.1.15, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.15, Copyright (c), by Zend Technologies

If you didn't add it, or you want to use a different interpreter, you can set a custom path in the php.validate.executablePath directive.

At the time of writing there isn't a visual editor for this setting so you'll need to edit it as JSON. Windows directory separator (\) is a special character in JSON, so you need to either escape it or use Unix separator (/)

"php.validate.executablePath": "C:\\PHP\\php.exe"
"php.validate.executablePath": "C:/PHP/php.exe"
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
-1

Open Visual Studio Code settings and verify the intelephense.environment.phpVersion directive is set to "8.1.0".

S.B
  • 13,077
  • 10
  • 22
  • 49