Articles

How To Fix Syntax Error?

by Wewp You Wewpyou

What is a Syntax Error?

A syntax error occurs when there’s a mistake in your code. As a result, the compiler cannot process (parse) the file and fail to display your website.

On WordPress, syntax errors are usually caused by incorrect lines in PHP scripts. Some common mistakes are incorrect coding structure, missing punctuations, invalid variables, and wrong function names.

What are Parse Errors?

In the context of PHP, parsing is what happens when your code is being converted from a series of characters to the final result that visitors are going to see on your site.

These errors occur when the PHP code can’t start or finish being parsed for one reason or another. It could be that the file or database is missing, corrupt or broken, your version of PHP isn’t compatible with WordPress, your file permissions aren’t set correctly, your server ran out of PHP memory or there’s a structural error in the code.

In WordPress, the latter is typically what you may come across the most and there are three main kinds:

  • Syntax error – There are problems with the semicolons, or curly brackets or quotations that were used. Either their missing or the wrong ones were included.

  • Unexpected error – You forgot to include a character such as an opening or closing bracket or other possible characters.

  • Undefined constant error – A character is missing in an array, when referencing a variable or other possible scenarios.

Keep in mind that these three kinds of parsing errors have many different variations depending on the specific mistake in the code. Read on for more details on this and how to fix these errors.

Fixing Parse Errors

Parse errors may seem terrifying, but they’re one of the simpler errors to resolve. Somewhere in the code you just added, there’s a character missing or out of place and if you open the offending file and correct it, the error goes away.

If you just added code yourself or you installed a plugin or theme before the error occurred, that’s where you need to look to fix the issue. Removing the code, plugin or theme should resolve the issue or you can go ahead and fix it yourself.

There’s a great way to narrow down where the cause of the error lies since the full error codes give you a great hint of where to look.

Below are the basic structures of the full parse error codes mentioned above that you’re most likely going to come across.

Syntax errors have this basic structure:

“Parse error: syntax error, unexpected character in path/to/php-file.php on line number“

Undefined constant errors are structured this way:

“Notice: Use of undefined constant constant string – assumed ‘constant string‘ in path/to/php-file.php on line number“

Unexpected errors can have different variations, but generally look like the example below:

“Parse error: unexpected character in path/to/php-file.php on line number“

Keep in mind that for undefined constant errors, the named constant string may not actually be a string since PHP often refers to unidentified tokens as constant strings.

Similarly, unexpected parse errors may list a string instead of a character.

In each of these examples, number refers to the line number and is going to be replaced with actual numbers such as 23 or 1256, for example, though, not necessarily those exact line numbers.

The file indicated in the error message is where the issue persists and the line number gives you a hint as to where to look for the mistake. It’s either on that line or a little bit before it.

The character or constant string names in these structure examples give you an idea of what to look for around the indicated line number. These are what needs fixing or else they’re related to what needs correcting.

Now that you know where and what to look for, you can log into your site using your preferred FTP client to locate the file and either edit it directly in your FTP client or you can download it to your computer, edit it, then re-upload it back to your site.

For details on using FTP, check out one of our other posts How to Use FTP Properly with WordPress.

Avoiding these Errors

Avoiding these errors is as easy as fixing them if you’re at least a little familiar with PHP. You can check your code for syntax mistakes automatically by running it through a PHP code validator.

Here are some free validators you can use:

  • W3 Markup Validation Service

  • PHP Code Checker

  • PHP code Syntax Check

Here are some tips of the most common syntax mistakes to look out for right off the bat:

  • Make sure there’s only one opening (<?php) and closing tag (?>)per document

    • Look for opening and closing tags in the middle of a file

    • If you add code, be sure it’s included before a closing tag and not after it

  • Add code shouldn’t be inserted in between a function

    • Check for functions that are broken up by other ones

For details on PHP syntax, check out these resources:

  • Basic PHP Syntax

  • PHP Tags

  • Escaping from HTML

  • Instruction Separation

  • Comments

You can also check out and the PHP Validation Manual for more details of how to validate your syntax.


Sponsor Ads


About Wewp You Junior   Wewpyou

1 connections, 0 recommendations, 14 honor points.
Joined APSense since, June 24th, 2021, From New york, United States.

Created on Aug 12th 2021 01:26. Viewed 305 times.

Comments

No comment, be the first to comment.
Please sign in before you comment.