3

I have a large php project and different developers work on the same project. Changes in php file e.g syntax error can lead to 500 internal server error if another developer tries to run the same project - leaving the other developer clueless as to where the error is from. I need to download some batch file that checks the whole project and displays the line numbers and errors that occured for each file in the project and not just in one file e.g. when using php -l filename - instead I want it to be php -l project

michelle
  • 2,759
  • 4
  • 31
  • 46
  • `php -l project` will never catch any errors that are caused by runtime conditions, so it will probably not be very useful. – Pekka Dec 28 '11 at 11:22

3 Answers3

9

If you are using linux, this command will check your current folder recursively for all php files, syntax check them and filter out the ones that are OK:

find . -name \*.php -exec php -l {} \; | grep -v "No syntax errors"

You'll get a nice list of files and errors.

Maerlyn
  • 33,687
  • 18
  • 94
  • 85
  • Thanks, this is so simple and allowed me to find a syntax error (of mine) that occurred on 7.2 and not 7.4. Note of course it checks using the system's PHP version. – Duncanmoo Jan 19 '22 at 11:36
4

Edit: Turns out the OP is looking for a way to activate error reporting. I'll leave this in place anyway because I'm sure it's good universal advice for many in similar situations.

I don't know your situation, but to me, it sounds like what you might really need is a proper deployment process using at least a version control system. Multiple developers working on the same files simultaneously without any version control is a recipe for disaster, that much I can guarantee you.

Some starting points:

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • we already have that in place. what we need to do ultimately is be able to check for syntax errors for the whole project – michelle Dec 28 '11 at 10:59
  • 1
    @michelle are you looking to activate error reporting then? You can have PHP errors displayed on screen instead of showing a 500 error. Is that what you're looking for? – Pekka Dec 28 '11 at 11:00
  • 1
    @Michelle ah, okay. Look into the `error_reporting` and `display_errors` PHP.ini options, activating both should give you the errors directly. – Pekka Dec 28 '11 at 11:03
3
$it = new RecursiveIteratorIterator( new RecursiveDirectoryIterator('.'));
$regx = new RegexIterator($it, '/^.*\.php$/i', // only matched text will be returned
    RecursiveRegexIterator::GET_MATCH);
foreach ($regx as $file) {
    $file = $file[0];
    exec('php -l ' . $file); //check the syntax here
}
Ron
  • 22,128
  • 31
  • 108
  • 206
maxjackie
  • 22,386
  • 6
  • 29
  • 37