Checking PHP syntax with lint and how to do it in a string, not in a file

Firstly, I am confused about how to run PHP on the command line. I read several articles on the Internet, and they all say that you need a command line interface (CLI).

Basically, I have PHP files, and I want to name something like this:

php -l somefile.php 

But I want to check the line, not the file! How can this be done? Can you use STDIN, STDOUT or STDERR?

If so, how? Can someone give an example here?

Also, where can I post this code above? I donโ€™t have access to the command line (I donโ€™t think), or just put it in a PHP file that will be launched? Will he execute this code in this case on the command line?

I totally donโ€™t understand how this function of the PHP command line works ... Can someone help shed light on this for sure?

+8
command-line php syntax-error
source share
3 answers

If you need lint code (not inside the file), the only option is to write a wrapper.

Assuming your $ HOME / bin precedes / usr / bin, you can set your shell to $ HOME / bin / php, which has another option for the command line. The wrapper will create a temporary file, put the code there, run /usr/bin/php -l file , and then delete the temporary file.

NTN.

+4
source share

You can check the code with php -l from STDIN by plugging it in. Example:

 $ echo "<?php echo 'hello world'" | php -l Parse error: syntax error, unexpected end of file, expecting ',' or ';' in - on line 2 Errors parsing - 

Here is the ending semicolon ; missing after a single quote. If you add it, the error will disappear and PHP will tell you the following:

 $ echo "<?php echo 'hello world';" | php -l No syntax errors detected in - 

Dash - in Errors parsing - or No syntax errors detected in - means STDIN. It is commonly used for this.

Another way is to write the code that you want to grope yourself (or copy and paste it). This works by using the lint switch with -- , entering the code and ending it by typing Ctrl + D (Linux) / Ctrl + Z (Win) on a separate line:

 $ php -l -- <?php echo "1" ^Z Parse error: syntax error, unexpected end of file, expecting ',' or ';' in - on line 2 Errors parsing - 

BTW, the -r switch, which is usually designed to provide code for execution, does not work in this case, and it gives an error:

 $ php -l -r "echo 1" Either execute direct code, process stdin or use a file. 

Most likely, because it is designed to run code, and for this it is not used. Also, this is without opening the PHP tag.


Of all these options, the first of them probably makes the most sense if you want to connect it (you can also work with proc_open if you need more control). Here is a quick example using PHP exec :

 <?php /** * PHP Syntax Checking with lint and how to do this on a string, NOT a FILE * * @link http://stackoverflow.com/q/12152765/367456 * @author hakre */ $code = "<?php echo 'hello world'"; $result = exec(sprintf('echo %s | php -l', escapeshellarg($code)), $output, $exit); printf("Parsing the code resulted in; %s\n", $result); echo "The whole output is:\n"; print_r($output); 

The output is as follows:

 Parsing the code resulted in; Errors parsing - The whole output is: Array ( [0] => [1] => Parse error: syntax error, unexpected '"', expecting ',' or ';' in - on line 1 [2] => Errors parsing - ) 
+14
source share

I would suggest checking phpstan . This is a linter built using PHP, and works just like phpunit, but for linting code. You can write configs, line control levels and enable / disable folders.

It also has the correct exit codes.

https://github.com/phpstan/phpstan

If this is not possible, I used this script here and it works well.

+1
source share

All Articles