PromptHub
PHP Beginner 18 views

Parse Error: Syntax Error

PHP detected a syntax error in the source code that prevents parsing.

Explanation

A parse error (syntax error) occurs when PHP encounters code that violates the language's grammar rules. This is a fatal error that prevents the script from executing at all. Common causes include missing semicolons, unmatched parentheses or braces, incorrect use of language constructs, and typos in function or class names. PHP reports the file and line number where the syntax error was detected, though the actual error may be on a preceding line.

Common Causes

  • Missing semicolon
  • Unmatched brackets or parentheses
  • Incorrect language construct usage
  • Typo in keyword
  • Missing comma in function parameters

Solution

Carefully check the line number indicated in the error message, but also look at the lines above it since syntax errors are often reported at the point PHP first noticed the problem. Use an IDE with PHP syntax highlighting and error detection to catch these issues in real-time. Verify all brackets, parentheses, and braces are properly matched. Check that all statements end with semicolons. Use PHP-CS-Fixer or PHP_CodeSniffer to automatically detect formatting issues. Run php -l filename.php to syntax check files without executing them.

Code Example

// Missing semicolon - causes parse error
$users = DB::table('users')->get()  // <-- missing semicolon

// Fix
$users = DB::table('users')->get();

// Unmatched parenthesis
function getUsers($id {
//                   ^ missing closing paren
    return User::find($id);
}

// Fix
function getUsers($id) {
    return User::find($id);
}

// Check syntax without running
php -l app/Http/Controllers/UserController.php

Error Information

Language

php

Difficulty

Beginner

Views

18

Related Errors