search
HomeBackend DevelopmentPHP TutorialSummary of tips in PHP development

The following editor will bring you a summary of some tips in PHP development (a must-read article). The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.

1. Development habits and php code

1. Accurate understanding Various concepts. Nowadays, new things are emerging in an endless stream. Literal meaning and a little knowledge are harmful to development work; //For example, I met someone who understood the concept of loose coupling (this thing is not new) and actually required that the code should not have blank lines, otherwise your program would be "too "Loose"

2. The code is beautiful, with appropriate blank lines, indentations, and spaces, so that it is easier to understand the meaning of the code segment;

3. Be sure to write comments, and they must be appropriate comments, otherwise the subsequent maintenance work or the person who takes over the code will cry bitterly;

4. Static methods, class access rights, interfaces, and abstract classes should be used together to give full play to their respective characteristics;

5. Do not copy and paste. Even if you want to use ready-made code, you must review it line by line before adding it to a new project, because experience tells us that this is too error-prone. For large sections such as open source classes, The code needs more;

6. Variables must be initialized;

7. Don’t just deal with errors and ignore warnings and notices. This may lead to inexplicable problems in the future. The project is in the development state The following should be error_reporting(E_ALL ^ ​​E_NOTICE). When the external network production environment is released, all error reports should be turned off display_errors=Off,error_reporting(0)

8. Record some necessary error logs, such as writing files Failure, failure to write to memcache, failure to connect to socket, failure to read and write to the database, logs can help quickly locate problems when problems occur. I personally strongly recommend turning off all error reports in external production environments;

9. Use try, catch captures exceptions, which is helpful for the robustness of the code. It is often encountered in the API interface, which makes it much more friendly;

10. It is recommended to add curly brackets to variables appearing in double quotes. As for "$ {nider}at gmail.com" or "{$tom}at zendstudio.net" depends on personal habits. I prefer the latter one;

11. Try to have as few if else nesting levels as possible. Maybe you It is necessary to express a very complex logical algorithm, but doing so can at least make the code logic clearer

12. Read more excellent codes of online open source projects (not open source codes of excellent projects), and learn from them what is worth learning from

13. How pleasant it is to use sprintf formatting for language packages!

14. When writing cache, you do not always need to serialize it first.

15. When AJAX transmits data, do not directly json_encode the array found in the database and then pass it to the client. Do this Not only is there a certain security risk (field names are exposed), but some unnecessary data is transmitted out and wastes bandwidth. This also applies to API interfaces

16. Remember to deal with magic variables. My method is to directly Close, of course, you can also get the switch status to avoid the problem of transmitting data being processed twice

17. Use $GLOBALS[\'var\'] instead of global $var

18. Don’t do it easily Die out the program, especially inside the method

19. require, require_once, include, and include_once have slightly different application scenarios

20. In order to maximize the success of the write cache, you can Combining the number of retries + usleep, I usually retry 3 times. If it still doesn’t work, write down a log.

21. PHP constants are a very good thing. Many open source projects use an entire file to define them. Constants to be used

22. Use absolute paths as much as possible to find files

23. Autoload is a very flexible thing

24. It is best to use set_error_handler and set_exception_handler , which makes your project more perfect

25. PHP’s reference type is very efficient. It is recommended to use

26 when performing complex operations. The @ symbol is very performance-intensive to suppress errors. Therefore, try to find alternatives as much as possible

2.MYSQLPart

1. Use double quotes in SQL statements, where The values ​​​​are all in single quotes, for example "INSERT INTO gril SET money='{$iMaxMoney}',age='18′"

2, use mysqli extension instead of mysql extension

2, Use mysqli_real_escape_string and mysqli_escape_string to process variables in outgoing sql statements

3. Use mysqli_set_charset(mysqli->set_charset) instead of query "SET NAMES"

4.Union query## Before #(JOIN), consider the amount of data in each table. If it is not appropriate, you should check it separately, especially when cache is available.

5. Many places need to record the occurrence time, but not every table needs it. , similarly, not every table needs an auto-increment primary key

6. Many times it is good to add unsigned to the

integer type

7. INERT DELEYED, INSERT IGNORE, SELECT DISTINCT...this kind of statements usually have unexpected good effects

8. The varchar type does not have a length that cannot exceed 255, but if it exceeds 255, this field cannot be indexed. Therefore, it depends on your actual needs.

That’s all I can think of for the time being. I’ll wait for more. Keep updating as you think of it. I write whatever comes to my mind, there is no structure, so please bear with me. If this helps you at all, then I will be very happy.

The last piece of ultimate advice is - communicate more with others to make faster and greater progress! Welcome to communicate with me and leave your valuable opinions.

The above is the detailed content of Summary of tips in PHP development. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools