How to do syntax checking using PHP ?
Last Updated :
21 Sep, 2021
Syntax checking is one of the most important tasks in programming. Our compiler checks our code and shows relevant errors if there is any in the code i.e. compile time, run time, syntax, etc. We can do the same thing i.e. syntax checking in PHP.
In this article, we are going to learn how we can do syntax checking in PHP. The syntax is basically a set of rules that defines the structure of a programming language. We can not manually change the syntax and we have to follow the rules of the language.
Syntax:
php -ln filename
We can use –syntax-check in place of -ln, it is more readable.
php --syntax-check filename
It returns “No syntax errors detected in <filename>” if the provided PHP file has no syntax errors and passed the syntax check. Otherwise, it returns “Errors parsing <filename>” if there is a syntax error in the PHP file.
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. It finds the sum of two numbers.
PHP
<?php
$x = 10;
$y = 20;
$c = $x + $y ;
echo "Sum is: " , $c ;
echo "\n"
?>
|
Command for syntax checking:
php -ln /home/rich/Documents/geeks/geek.php
Output:
No Syntax Error
Example 2: In the above code, we simply replaced the “Sum: “, $z; with “Sum:, $z; then it throwing a syntax error.
PHP
<?PHP
$x = 15;
$y = 30;
$z = $x + $y ;
echo "Sum:, $z ;
echo "\n" ;
?>
|
Command to do syntax checking:
php -ln /home/rich/Documents/geeks/geek.php
Output:
Syntax Error
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...