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
185
Related Errors
PHP
File Upload Error
PHP failed to process a file upload due to size limits, permissions, or configuration issues.
Laravel
Laravel Missing Application Encryption Key
The APP_KEY is not set or is invalid, preventing encryption operations.
Database
Duplicate Entry
The code tries to insert a record with a value that already exists in a unique column.
Database
Lock Wait Timeout Exceeded
A database query waited too long for a lock held by another transaction.