1. Use the ip2long () and long2ip () functions to convert the IP address into an integer and store it in the database. This method reduces the storage space to nearly 1/4 (15 bytes of char (15) to 4 bytes of integer ), it is easier to calculate a specific address on a page in a specific segment, and it accelerates search and sorting (although sometimes only a little faster)
1. Use the ip2long () and long2ip () functions to convert the IP address into an integer and store it in the database. This method reduces the storage space to nearly 1/4 (15 bytes of char (15) to 4 bytes of integer ), it is easier to calculate a specific address on a page in a specific segment, and it accelerates search and sorting (although sometimes only a little faster)
1. Use the ip2long () and long2ip () functions to convert the IP address into an integer and store it in the database.
This method reduces the storage space to nearly 1/4 (15 bytes of char (15) to 4 bytes of integer ), it is easier to calculate whether a specific address is on a page in a specific segment, and it accelerates search and sorting (although sometimes only a little faster ).
2. When verifying the Email address, use the checkdnsrr () function to verify whether the domain name exists.
This built-in function can confirm that the specified domain name can be resolved to an IP address. The user comment section of the PHP document for this function has a simple user-defined function, which is based on checkdnsrr () to verify the legitimacy of the email address. For those who think their e-mail addresses are memory@wwwphp100.net rather than memory@php100.net, this method can easily catch them.
3. If you are using PHP 5 and MySQL 4.1 or later, use the mysqli _ * series functions.
A good feature is that you can use preprocessing statements. If you are maintaining a database-intensive site, this feature can speed up the query. Some evaluation scores.
4. Learn to fall in love with Ternary operators.
5. If you feel reusable parts in the project, check whether PEAR already exists before writing a line of code.
Many PHP programmers know that PEAR is a good resource library, although many programmers do not know. This online resource library contains more than 400 reusable program fragments that you can immediately use in your program. Unless your project is really special, you can always find the PEAR package that helps you save time.
6. Use highlight_file () to automatically print the beautifully formatted source code.
If you seek help from a script on the message board or IRC, this function is very handy. Of course, be careful not to accidentally disclose your database connection information and password.
The highlight_file () function highlights the syntax of the file.
Php code
- Highlight_file (filename, return)
Filename |
Required. Path of the PHP file to be highlighted. |
Return |
Optional. If set to true, this function returns the highlighted code. |
This function uses PHP syntax to highlight the color defined in the program, and outputs or returns the color contained inFilenameThe highlighted version of the code syntax.
Many servers are configuredPhpsThe suffix file is automatically highlighted. For example, when you view example. phps, the source code highlighted by the syntax of the file is displayed. To enable this function, add the following lineHttpd. conf:
Java code
- AddType application/x-httpd-php-source. phps
Return Value
IfReturnIf the parameter is set to true, the function returns the highlighted code instead of outputting them. Otherwise, true is returned if the operation is successful, and false is returned if the operation fails.
Instance:
Php code
- "Font-size: small;">
-
-
- Highlight_file ("test. php ");
- ?>
-
-
7. Use the error_reporting (0) function to Prevent Users From seeing potential sensitive error messages.
Ideally, the publishing server should be completely disabled in php. ini. However, if you are using a shared web server, you do not have your own php. ini file. In this case, you 'd better choose to add error_reporting (0) before the first line of all scripts (or use the require_once () method ). This completely blocks sensitive SQL query statements and path names when an error occurs.
Error_reporting () sets the PHP error level and returns the current level.
Mask Value
1 E_ERROR
2 E_WARNING
4 E_PARSE
8 E_NOTICE
16 E_CORE_ERROR
32 E_CORE_WARNING
E_NOTICE indicates that it is not recorded in general cases and used only when the program has an error, for example, an attempt to access a non-existent variable or call the stat () function to view a non-existent file.
E_WARNING is usually displayed, but the execution of the program is not interrupted. This is effective for debugging. For example, call ereg () with a problematic regular expression ().
E_ERROR is usually displayed and the program execution is interrupted. This mask cannot be used to trace memory configurations or other errors.
E_PARSE parsing errors from syntax.
E_CORE_ERROR is similar to E_ERROR, but does not include errors caused by the PHP core.
E_CORE_WARNING is similar to E_WARNING, but does not include PHP core error warnings.
8. Before storing large strings in the database, use gzcompress () and gzuncompress () to explicitly compress/decompress the strings.
This PHP built-in function uses the gzip algorithm to compress common text by up to 90%. These functions are used every time I want to read and write BLOB fields. The only exception is when I need full-text retrieval.
String gzcompress (string data [, int level])
Note:
Returns the compressed version of the input value string. If the input value fails, false is returned. The level of the non-required parameter can be 0 to 9, 0 indicates no compression, and 9 indicates the maximum compression.
However, this compression is not gzip compression.
9. Multiple return values are obtained from a function using the "Reference" parameter passing method.
Like the ternary operator, most programmers who have been formally trained in programming know this technique. However, programmers whose HTML background is greater than the Pascal background have more or less such questions: "How many values can be returned from a function when only one return can be used ?" The answer is to add the "&" symbol before the variable and pass it through "Reference" instead of "value.
10. fully understand the risks of "Magic quotes" and SQL injection.
I hope all the developers here will be familiar with SQL injection. However, I still put this article here because it is hard to understand.
Magic quotes are a process of automatically escaping data from PHP scripts. It is best to escape the code instead of escaping it as needed during runtime.
What is magic quotes?
When open, all '(single quotes), "(double quotation marks), \ (backslash), and NULL characters are automatically added with a backslash to escape. This works exactly the same as addslashes.
There are three magic quotes:
* Magic_quotes_gpc affects HTTP request data (GET, POST, and COOKIE ). It cannot be changed at runtime. In PHP, the default value is on. See get_magic_quotes_gpc ().
* If magic_quotes_runtime is enabled, most functions that obtain data from external sources and return data, including data from databases and text files, will be escaped by backslash. This option can be changed at runtime. The default value in PHP is off. See set_magic_quotes_runtime () and get_magic_quotes_runtime ().
* If magic_quotes_sybase is enabled, single quotation marks are escaped rather than backslash. This option will completely overwrite magic_quotes_gpc. If two options are enabled at the same time, the single quotation marks are converted ''. Double quotation marks, backslash, and NULL characters are not escaped. For how to obtain the value, see ini_get ().