Monday, June 15, 2026
HomeLanguagesHow to do syntax checking using PHP ?

How to do syntax checking using PHP ?

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/neveropen/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/neveropen/geek.php

Output:

Syntax Error

RELATED ARTICLES

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS