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

What are the pitfalls in PHP programming?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what are the pits in PHP programming?" in the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1, due to the use of single quotation marks, with "" as the separator, using the PHP function explode to split the string, can not be divided normally.

Reason: this involves the difference between single quotation marks and double quotation marks, in which the backslash cannot be parsed. Therefore, when using explode segmentation, if you use single quotation marks, it will be treated as a string, not a newline character, so it cannot be split properly at this time.

Similar problems include situations where {} is included in the string. In a string, for a variable contained in {} to be parsed successfully, the string must use double quotation marks.

2. Due to the BOM header, the json string cannot be parsed successfully by using the PHP function json_decode.

Reason: UTF-8-encoded files can be divided into two formats: no BOM and BOM. What is BOM? The three bytes of "EF BB BF" are called BOM,BOM. The full name is "Byte Order Mard". BOM is commonly used in utf-8 files to indicate that this file is a UTF-8 file, and the original intention of BOM is actually used to represent a sequence of high and low bytes in utf16. A BOM before a byte stream means a low-byte sequence (low-byte comes first), while utf8 doesn't have to worry about a byte sequence, so it doesn't matter whether you have BOM or not. UTF-8 uses bytes as the encoding unit, and there is no byte order problem. UTF-16 uses two bytes as encoding units. Before interpreting a UTF-16 text, you must first figure out the byte order of each encoding unit. For example, the Unicode code of "Kui" is 594E, and the Unicode code of "B" is 4E59. If we receive the UTF-16 byte stream "594E", is this "Kui" or "B"?

If you choose to use BOM when saving the file, it will make the page display improperly. Generally speaking, php does not support BOM, and php files should be saved as UTF-8 without BOM type, so do not use BOM when saving UTF8-encoded PHP files.

3. Due to the forward and backward slashes, the use of the PHP function basename is invalid

We often use the PHP function basename to get the basic file name from a string that contains the full path to a file, but sometimes you will find that the basename function does not work because of the forward and backward slashes, especially when switching between window and linux systems. It turns out that the basename function is affected by the operating system, and when used in Windows, both the slash (/) and the backslash () can be used as directory delimiters, while in other environments they can only be slashes (/). Therefore, if you use a backslash () in window, there will be problems with other systems.

To avoid this effect, * * uses a slash (/) as the directory separator. If a namespace is used, * * first uses the str_replace function to replace the backslash () with a slash (/).

4. Excessive removal of trim series functions

The basic use of the trim function is to remove the outermost spaces, newline characters, and so on. Because of its optional parameters, many people also use it to remove UTF8BOM headers, file extensions, and so on, such as ltrim ($str, "\ xEF\ xBB\ xBF"); rtrim ($str, ".txt");. But soon, you will find that these functions will remove a few more things, such as when you were supposed to remove the suffix, the logtext.txt will become logte instead of logtext. Why? Because the latter parameter does not mean a complete string, but a list of characters, that is, it always checks whether the leftmost / rightmost matches one of the lists.

5. Htmlspecialchars function does not escape single quotation marks by default

Many websites use this function as a general input filtering function, but this function does not filter single quotation marks by default. This is very easy to cause XSS vulnerabilities. This is not much different from not filtering double quotation marks, as long as the front end is a little irregular (using single quotes). Therefore, we must add the parameter htmlspecialchars ($data, ENT_QUOTES) to this function when we use it.

6. The retention phenomenon of foreach

When using a usage such as foreach ($someArr as $someL) {}, be aware that a $someL of * * is retained until the end of the function / method. When using references, foreach ($someArr as & $someL) {} this is saved as a reference, which means that if there is a variable name with the same name, it will change the original data (like a misused C pointer). For security reasons, it is recommended that you use unset to clear these variables after each foreach (especially the referenced one) ends.

7. Decimals (symbols) cannot directly compare whether they are equal or not.

For example, the result of if (0.5, 0.2, 0.2, 0.7) is false. The reason is that PHP is based on C language, and C language can not accurately represent the number of most characters because of its binary point representation. In fact, almost all programming languages fail to accurately represent decimals (symbol numbers), which is a common phenomenon because this is a flaw in IEEE 754. If you want to solve this problem, you have to set another standard, and it seems that only Mathematica has solved this problem.

8. Whether the string is the same, it is recommended to use = instead of = =

Why? Because this comparison is a weak type. When comparing the two, PHP first tries to determine whether the left and right are numbers. The question is what kind of string is a number, is it a simple string of numbers? Much more than that, it also includes hexadecimal with 0x, scientific notation of XXeX type, and so on, for example, '12e0' gets true. 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.

So in these cases, it may cause otherwise different strings to 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.

Also complain that JAVA,== can't compare whether a string is equal, because a string is an object, and = = becomes to judge whether it is the same object or not.

9. Case in switch cannot be used as if

In the PHP function switch. In case, switch matches the value of the case statement, but case cannot be used as an if. At the same time, switch expressions give priority to matching case statements that match their value types, and inconsistencies are dealt with later, as follows:

10. The strrchr function looks for a character, not a string

The explanation of the strrchr () function in the PHP manual is to find the position of the string that appears once in another string and return all characters from that position to the end of the string. If it fails, false is returned. In fact, this function looks for a character, not a string. In the following example, many people must have thought they would return false at first, but they didn't.

This is the end of the content of "what are the pits in PHP programming". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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