Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code - does not recognize php 8 syntax

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.

like image 221
ConfusedProgrammer Avatar asked May 30 '26 12:05

ConfusedProgrammer


1 Answers

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"
like image 129
Álvaro González Avatar answered Jun 02 '26 02:06

Álvaro González



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!