Troubleshooting PHP Errors when Adding Custom Code

There are some common mistakes that beginners make when adding custom code snippets to their websites. Let’s take a look at some of them and how to avoid them

1. Incorrect Usage of PHP Begin and End Tags

WordPress is written mainly in the PHP programming language which has a specific syntax that tells your server that the following code needs to be processed by PHP. Here is how a typical PHP code snippet looks like:

Tables can't be imported directly. Please insert an image of your table which can be found here.

1 2 3 4 5 6 7    

All your PHP code needs to be inside the <?php and ?> tags.

The PHP end tag becomes more important in files that switch back and forth in HTML. This includes most WordPress themes files which use PHP tags alongside HTML.

You need to make sure that if you are pasting your code at a location where PHP start tag is not closed then you need to add your code without the starting PHP tag.

Tables can't be imported directly. Please insert an image of your table which can be found here.

1 2 3 4 5 6    

If you are pasting your custom code outside or after the PHP end tag, then you need to add the PHP begin tag as well.

Tables can't be imported directly. Please insert an image of your table which can be found here.

1 2 3 4 5 6 7 8    

Almost 90% of all errors are caused by incorrect placement of PHP end or start tags. Studying the code will allow you to understand whether or not you need to add the PHP begin or end tags in your custom code snippet.

Many WordPress theme files particularly functions.php file may not have a PHP end tag at all. This means that you can add your code at the bottom of the file without the start or end tags.

Tables can't be imported directly. Please insert an image of your table which can be found here.

1 2 3 4 5 6 7 8 9 10 11

Remember that tutorial writers may sometime assume that you already know how to use PHP start and end tags. They may simply show you a code snippet without those tags.

Tables can't be imported directly. Please insert an image of your table which can be found here.

1 2 3 4 5 6

Since the tutorial author doesn’t know where you will be adding this code, they have skipped the PHP end and start tags. Now when you are adding such a code snippet in your theme files, you need to make sure that it is inside the PHP tags.

2. Incorrect Nesting Errors

PHP has a particular syntax for functions, conditional logics, and loops. This syntax depends on curly brackets which indicate when a function begins and when it ends.

For example, here is a simple PHP function:

Tables can't be imported directly. Please insert an image of your table which can be found here.

1 2 3 4 5

Now if you want to add a custom code snippet that has nothing to do with this function, then you will need to put it outside this function like this:

Tables can't be imported directly. Please insert an image of your table which can be found here.

1 2 3 4 5 6 7 8 9 10 11 12

If you miss the starting or ending curly brackets, then this will break the code, and you will end up with an error page.