Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to avoid stepping on the pit PHP

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to avoid trampling on the PHP. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

The editor sorted out the possible pitfalls in the learning and development of novice friend php. I hope I can give you some guidance. Don't continue to do these things in development.

Edit the php file with notepad

In the early years, being able to program with notepad is the capital that some people boast about themselves, and being able to program with notepad is synonymous with greatness. But here's a word of caution: never use the notepad that comes with Windows to edit any text files. Editing a php file with Windows notepad can cause some unexpected errors because the bom headers it adds to the file are output as content when executed by php.

Php does not support BOM, and php files should be saved as UTF-8 without BOM types, so do not use BOM when saving UTF8-encoded PHP files.

Mask error messages with @

Blocking error messages with @ is very inefficient, and we can use error_reporting (0) to turn off error reporting. If you find it troublesome to write each line in this way, you can also configure the php.ini file and search for display _ errors=on. By default, on means to turn on the error report function and change it to off.

In fact, we should face up to all error-level errors in programming. In the development environment, we deal with all errors of attention (Notices), warning (Warnings), and fatal error (Fatalerrors). Now that thinkphp and laravel are enabled by default, error_reporting (E_ALL) is enabled, so let's not cover our ears and use @ to screen error messages.

Compare variables with = =

Whether the string is the same or not, it is recommended to use = instead of =, because this comparison is a weak type. When comparing the two values, PHP first tries to determine whether the left and right values are numbers. When comparing a numeric type with a string, even some non-numeric strings at the beginning of a number, such as the string'12', will get a value of true. In these cases, otherwise different strings may be judged to be equal. The use of = = comparison is a comparison of containing types, and there is no conversion, so you can accurately compare whether the string is the same.

Ignore the difference between single quotes and double quotation marks

PHP searches for variables in strings enclosed in double quotes, but not in single quotes. The fields in double quotes will be interpreted by the compiler and then output, while those in single quotes will be output directly without interpretation. If the output string has the same name as the variable, you must use single quotation marks to avoid parsing.

Using the family of mysql_ functions that have retired from the stage of history

PHP7 has removed all the mysql_ functions from the core, you need to open the mysqli extension or the pdo_mysql extension, and if you are using Laravel, you must open the pdo_mysql extension. That means you need to migrate to the mysqli_ function, or a more flexible PDO implementation.

Use PHP closing tags at the end of the file

The tagging section in the php manual tells us that php code begins and ends with. However, if the content is a pure PHP code file, it is best to remove the PHP closing tag at the end of the file. Can this avoid the end tag in PHP? > after that, if spaces or newline characters are accidentally added, it will cause PHP to start to output these blanks, and there is no intention to output them in the script at this time.

PHP does not require a closed tag at the end of the file, and we can remove it to ensure that no white space characters are added later. This is why we write php code in html templates, while writing php class files only ends.

On how to avoid trampling on the PHP to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report