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 $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 - )