PHPMatters Help You Better Hosting Your PHP-based Sites
Common Mistakes for Pasting PHP Code Snippet

Common Mistakes for Pasting PHP Code Snippet

We have come out a series of PHP coding stuff achieving different purposes, such as resizing images and creating WordPress category template. It looks like an easy task to copy and paste the existing PHP code as you just need to select the target lines of code, right click you mouse to choose the Copy option, and then paste the content into the right file. As investigated, however, many newbies who lack the most basic PHP knowledge of how PHP works have no idea how to paste the coding stuff correctly without affecting the whole website.

To help our readers avoid this embarrassment, we have listed some common mistakes in the following, concerning about properly pasting PHP code snippet from tutorials or examples to your own website files.

Mistake No.1

This mistake commonly happens among webmasters who know nothing about PHP. The basic semantics of PHP is as following, consisting of an opening tag and a closing tag. Some careless webmasters, however, simply forget to accomplish the code by lacking one of the two tags during the pasting process.

php format

Mistake No.2

The second commonly made mistake is closing the code on the wrong spot. Here, we have presented a wrong example.

close code wrongly

If you paste the code in this way, then it means you have opened a PHP tag properly, but fail to close it successfully. Note that if there are 2 codes attached together, then you have to close them one by one like the example showed in the following.

close code orderly

In addition, if the codes can be combined without affecting the final result, then you also can remove the additional opening tag and closing tag. Check the example in below.

close code together

Mistake No.3

Different coding editor has different writing habit and style. Some of them make the coding snippets attached with the PHP tags, and others simply assume you now the PHP semantics thus only present the core content like the following code.
no opening closing

In this circumstance, many webmasters only paste the code like the example presented above. You have to realize that if the code is out of the PHP tags, then it cannot be recognized, thus it breaks. The proper pasting should like the following.

Mistake No.4

The top 3 mistakes are about the wrong utilization of PHP tag, and the last common mistake goes for the improper function usage.


In the case that you need to make two functions get together, then you cannot paste one function inside another as this will cause your website break.

function name

Instead, you have to make them independently with their exclusive opening tag and closing tag.

function coding

Some Other Mistakes

In addition to the most common mistakes displayed in this tutorial, some webmasters may misuse the PHP coding in the following situations.


  • Paste the CSS code into the function files that include PHP tags.
  • Paste the PHP code into the style sheet of a .css file.
  • Paste the coding stuff in the wrong place such as the header and the footer scripts.
  • Paste the code that includes loop hooks while your website is still running the old XHTML markup.
  • Paste the PHP opening tag or closing tag into the function class.