Summary of Methods of php Syntax Checking

  • 2021-11-13 07:01:04
  • OfStack

We can use lint to check

Static check with lint.

Static is a method that only checks syntax description methods without executing programs.

Use the lint command at this time.

The syntax checking function php_check_syntax has been abolished, so it cannot be used.

Then prepare the php file with the actual error.

lint_test.php


<?php
echo "error"

It is just a code that displays error on the screen.

Move lint_test. php to a directory and issue the following command.


php -l lint_test.php

Execution results


PHP Parse error: syntax error, unexpected end of file, expecting ',' or ';' in lint_test.php on line 2 Parse error: syntax error, unexpected end of file, expecting ',' or ';' in lint_test.php on line 2Errors parsing lint_test.php

syntax error = Output syntax error indication.

It also returns an error row number of line 2.

Because there is unexpected end of file, there is no ";" in line 2. Is the reason.

Then, modify lint_test. php and execute the lint command again.


<?php
echo "error";

The execution result is:

No syntax errors detected in lint_test.php

Shows no syntax errors.

Using xdebug to dynamically check for syntax errors

First, enable xdebug.

Download xdebug from the official site below, and note the path of the local environment where the download.dll file is located.

https://xdebug.org/download.php

Add the following to php. ini.


zend_extension =  Path recorded in ① 

② Restart Web server (Apache, etc.)

This completes the setup.

Check for errors using xdebug

We run the lint_test. php used above.

lint_test.php


<?php
echo "error"

There was 1 error because there was no semicolon in the end.

The content is the same as it was when lint was executed, but with a little trim added to make it easier to view.

The biggest difference with lint is the error that occurs after code execution, so it can be said that an error occurs due to dynamic checking.


Related articles: