Open In App

How to do syntax checking using PHP ?

Last Updated : 21 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 returnsNo 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
  
    // First number
    $x = 10;
  
    // Second number
    $y = 20;
  
    // Sum of two numbers
    $c = $x + $y;
  
    // Displaying the sum
    echo "Sum is: ", $c;
    echo "\n"
?>


Output

 
Sum is: 30

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
  
    // First number
    $x = 15;
  
    // Second number 
    $y = 30;
  
    // Sum
    $z = $x + $y;
  
    // Displaying sum
    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
Previous
Next
Share your thoughts in the comments

Similar Reads