First off, I'm confused on how to run PHP in the command-line. I've been reading several articles on the web and they all say that you need a CLI (Command Line Interface).
Basically, I have PHP files, and I want to call something like this:
php -l somefile.php
But I'm wanting to check a string, not a file! How can this be done? Can using STDIN, STDOUT, or STDERR help with this at all?
If so, how? Can someone provide an example here?
Also, where do I place that above code? I don't have access to a command-line (I don't think), or do I just place it within a PHP file itself that will run? Will it execute this code, in that case, within the command-line?
I'm completely clueless on how this PHP command-line thing works... Can someone please help shed some light on this exactly?
To check your code, you must copy and paste, drag and drop a PHP file or directly type in the "PHP code" online editor below, and click on "Check PHP syntax" button. You can see the user guide to help you to use this php checker tool.
The “-l” or “–syntax-check” argument performs a syntax check on the given PHP file. Example 1: The following PHP Code is without Syntax error.
phplint.com is a PHP Code Quality tool that checks your code for good PHP practices, as listed in the clearPHP reference. It uses the exakat engine (version 0.2. 4) to run the audit analysis. For every piece of code you submit, phplint.com warns you : Incompilable files.
You can check code with php -l
from STDIN by piping 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 the ending semicolon ;
is missing after the single quoted string. If you add it, the error goes away and PHP tells you so:
$ echo "<?php echo 'hello world';" | php -l
No syntax errors detected in -
The dash -
in Errors parsing -
or No syntax errors detected in -
stands for STDIN. It's commonly used for that.
Another way is to write the code you want to lint your own (or copy and paste it). This works by using the lint switch with --
, entering the code and finishing it by entering Ctrl + D (Linux) / Ctrl + Z (Win) on a line of its own:
$ 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 normally intended to provide code for executing, doesn't 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 intended for running code and thats it for, no linting. Also it is without the opening PHP tag.
From all these options, the first one makes probably most sense if you want to pipe it in (you could also operate with proc_open
in case you need more control). Here is a quick example using PHP's 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 -
)
If you want lint code (not within a file) the only option is to write a wrapper.
Assuming your $HOME/bin precedes /usr/bin, you could install your wrapper in $HOME/bin/php that has a different option for command-line linting. The wrapper would create a temporary file, put the code in there, run /usr/bin/php -l file
and then delete the temporary file.
HTH.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With