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.
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.
The second commonly made mistake is closing the code on the wrong spot. Here, we have presented a wrong example.
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.
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.
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.
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.
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.
Instead, you have to make them independently with their exclusive opening tag and closing tag.
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.