The Little Guy Posted October 26, 2009 Share Posted October 26, 2009 How can I do a syntax check on a php file, with out executing the file? I can't really find a good way to do this... anyone have any ideas? I would like to return and save to a variable the error message that was returned. Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/179124-syntax-check/ Share on other sites More sharing options...
Alex Posted October 26, 2009 Share Posted October 26, 2009 There's a website here which does syntax checking on php files, and the source code to that is available. But even that runs the code. Is there any particularly reason that you don't want to run the code? The way I'd do it is to use file_get_contents() using the URL (so you don't just get the PHP code; assuming fopen_wrappers is enabled) and grab what the script running is outputting. Quote Link to comment https://forums.phpfreaks.com/topic/179124-syntax-check/#findComment-945060 Share on other sites More sharing options...
taquitosensei Posted October 26, 2009 Share Posted October 26, 2009 if you have access to the command line php -l phpfile.php Quote Link to comment https://forums.phpfreaks.com/topic/179124-syntax-check/#findComment-945061 Share on other sites More sharing options...
The Little Guy Posted October 26, 2009 Author Share Posted October 26, 2009 if you have access to the command line php -l phpfile.php I have been doing that, but it isn't working properly... exec("php -l '$filename'", $opt); print_r($opt); I know the file ($filename) has errors in it, because I wrote it to have errors, and when it runs it outputs the errors (which I don't want). but... the above output looks like this: Array ( ) shouldn't it have the error message in it? Quote Link to comment https://forums.phpfreaks.com/topic/179124-syntax-check/#findComment-945063 Share on other sites More sharing options...
salathe Posted October 26, 2009 Share Posted October 26, 2009 php -l only checks the syntax of the file, it won't catch runtime errors. Are you sure you want just a syntax check (as your original post says)? Quote Link to comment https://forums.phpfreaks.com/topic/179124-syntax-check/#findComment-945067 Share on other sites More sharing options...
The Little Guy Posted October 27, 2009 Author Share Posted October 27, 2009 Now I'm thinking I don't want to, I think I now want to execute the file, and return the results as a string, so I can save it in a variable. Quote Link to comment https://forums.phpfreaks.com/topic/179124-syntax-check/#findComment-945073 Share on other sites More sharing options...
Alex Posted October 27, 2009 Share Posted October 27, 2009 Now I'm thinking I don't want to, I think I now want to execute the file, and return the results as a string, so I can save it in a variable. Then wouldn't the method I posted previously suffice? Quote Link to comment https://forums.phpfreaks.com/topic/179124-syntax-check/#findComment-945075 Share on other sites More sharing options...
The Little Guy Posted October 27, 2009 Author Share Posted October 27, 2009 I have been trying to use file_get_contents but, I can not seem to get it to work. It isn't parsing the php in the file and I don't know why... It is printing out the html, but the php is getting parsed as html too, and not php. Why? Quote Link to comment https://forums.phpfreaks.com/topic/179124-syntax-check/#findComment-945082 Share on other sites More sharing options...
taquitosensei Posted October 27, 2009 Share Posted October 27, 2009 because it's a string. Either save it as a .php file then include it or use eval but both are evil. They leave open huge security holes. Quote Link to comment https://forums.phpfreaks.com/topic/179124-syntax-check/#findComment-945090 Share on other sites More sharing options...
The Little Guy Posted October 27, 2009 Author Share Posted October 27, 2009 I am running this from the command line in an infinite loop, when I load a file that has an error in it, it breaks my loop, and I can't have that. I need the error to go into a string so I can do what ever I want to do with that error message. Quote Link to comment https://forums.phpfreaks.com/topic/179124-syntax-check/#findComment-945093 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.