Jump to content

Syntax checking


AggressiveFish

Recommended Posts

I'm using "php -l scriptName.php". When I run this when there are errors I get two lines back - one starting with "PHP Parse error:...." and the other the exact same thing except minus the leading "PHP ". Is there a way to suppress the first line that gets printed? Please don't suggest fixing the code so there are no errors. The purpose of using the syntax checker is for a learning to program application. The code needs to check a user's input when they make changes and I only need the second error line that gets displayed (as it looks to be what is returned from the command - STDOUT). The first line appears to print from inside the command. I run the command from within a script and the value from the command is returned to the calling script but it's the second line that is "returned", the first line just prints. Hopefully that makes sense. I just need to somehow suppress the first error line. On successful syntax checking I get the desired results - one line of output is received.

Link to comment
Share on other sites

The other forum is, how to put it, not that well thought out considering if you are a new user you have only a limited number of posts? I went to respond and couldn't. So I decided to go to another forum and I can't quite continue from where I left off on that one. You're telling me there is no solution for suppressing the first error message? So there are essentially two of the same things getting reported. There has to be some sort of workaround if not a straight up solution (i.e. flag). 

Link to comment
Share on other sites

Lol not intentionally ? 

PHP Parse error:  syntax error, unexpected '=' in C:\directory1\directory2\test.php on line 4

Parse error: syntax error, unexpected '=' in C:\directory1\directory2\test.php on line 4
Errors parsing C:\directory1\directory2\test.php

My apologies though. I was thinking the output printed was the same line twice (with the second line just missing the "PHP " start) but now I see the additional "Errors" line at the end. When I run the command in the following way :

my $variableFeedback = qx "php -l C:\directory1\directory2\test.php";

I receive the above feedback. The variable $variableFeedback actually holds the string from "Parse error" onwards. The first line above is actually generated somewhere within the program call. 

So if you run a script with garbage / errors you only get the portion I'm looking for? Interesting. How can I go about looking for what is causing this? Whether XDebug (sorry, not familiar - fairly new to PHP) is installed or not? I had hoped there was a flag I could just add onto the -l that turned off some setting. Thank you for the reply. If you can help out a bit further I'd appreciate it. ?

 

Link to comment
Share on other sites

Here is what the manual says about it.

 

Quote
-l --syntax-check

Provides a convenient way to perform only a syntax check on the given PHP code. On success, the text No syntax errors detected in <filename> is written to standard output and the shell return code is 0. On failure, the text Errors parsing <filename> in addition to the internal parser error message is written to standard output and the shell return code is set to -1.

 

Link to comment
Share on other sites

One of the lines comes from the engine is controlled by the display_errors ini setting.  These are printed on STDOUT.

The second line is from the -l setting showing the specific error it encountered. These are printed on STDERR.

So, you can do two different things. 

1) You could turn off display_errors to prevent one of the messages on STDOUT.  You can do this by passing -d display_errors=0

2) You could redirect either STDOUT or STDERR to /dev/null (or elsewhere) to ignore or capture them specifically.

 

Link to comment
Share on other sites

kicken .... actually a very awesome answer and so very, very close to what I'm looking for. If I run with the flag you have above the "PHP" starting string gets printed to the STDOUT but the other one doesn't. The thing is I don't have any control over the string but the string that is getting returned from the command I can because I assign it to a variable first. Is there a parameter to suppress the "PHP" starting string but leave the non-PHP starting line? That would be the optimum scenario. If it cannot be done do you know off hand a quick and easy way to switch the active directory I'm executing from? I supply the full path to the php script but I don't want the user seeing the full path in the error returned. I either need the error returned where I can null out that portion of the path in the variable OR I supply the file name only but I need to be execute from that directory then. Hopefully that makes sense :-) Thanks so much though, a very informative and useful response. 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.