PHP Dry Goods! ---a beginner must know something

Source: Internet
Author: User

What the beginner of PHP should pay attention to. Just do the development should be how to ensure that their code is not easy to complain, how to not let the test old to find the problem of old are approved. Come down and introduce the method.

Many beginners into the company, found very laborious, their own code to write old mistakes, the old error to make himself very embarrassed at the same time, but also let the leadership to doubt their ability to have problems, then this article will change your destiny.

The first step: variable inhibition

Many errors occur because variables are not defined, so how can you guarantee that your variables are always defined?

In fact, the normal code logic to write variables should be initialized, that is, you use the variable must be initialized, so that the error will not occur. But since this is a mandatory requirement for PHP, all of the programmers are not going to initialize it at any time, and that's all it takes to create this problem. So here we recommend that we use an indeterminate variable with the Isset method to judge. Make sure you're there before you use it, which can save a lot of hassle and others will feel your mind is very rigorous.

Step Two: Database problems

As PHP development, perhaps many times you are drinking MySQL deal with, however, many of the MySQL error is not about to. Let's make a simple analysis of the error-prone areas.

1. Table name error

Many times write SQL statements may be their own table name is wrong and do not know their own, so caused the error, then how to avoid their own old write the wrong table name.

Many people here feel superfluous: Because many popular frameworks have their own tests, this problem does not occur. But do not rely too much on the framework, SQL should write the time to write. So I suggest, you can scan the database, and then the table name all stored as a table name class, each call variable can be called, so that the table is guaranteed to exist, also suppresses errors, and easy to manage. (belong to personal opinion don't like to spray.) )

2. Field Error

field errors are in fact consistent with surface error handling. The same method is used. However, the field class is best inherited from the table name class, and then the field is best to write the type length to the zodiac in the class for easy verification. Like the people can refer to

Step Three: Error suppression

Many beginners rarely use the try---catch this error suppression, in fact, in PHP this is still very cool, plus throw throw your code more compatible, I hope everyone has a good habit.

Summary: Try to write this article, feel not write very well. Can only write their own thinking of things, did not think of or say wrong hope everyone understanding.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.