Some interesting tips in PHP

Source: Internet
Author: User
Tags pconnect
The difference between single quotation marks and double quotation marks is that single quotation marks are more efficient than double quotation marks, because double quotation marks are used to pre-process the content. For example, the $ value output character $ value and the $ value output variable $ value. The difference between char and varchar char is fixed length while varchar is variable length. char is mainly characterized by pre-distribution in storage mode.

The difference between single quotation marks and double quotation marks is that single quotation marks are more efficient than double quotation marks, because double quotation marks are used to pre-process the content. For example, the '$ value' output character $ value and "$ value" output variable $ value. The difference between char and varchar char is fixed length while varchar is variable length. char is mainly characterized by pre-distribution in storage mode.

The difference between single quotation marks and double quotation marks is that single quotation marks are more efficient than double quotation marks, because double quotation marks are used to pre-process the content. For example, the '$ value' output character $ value and "$ value" output variable $ value. The difference between char and varchar char is fixed length while varchar is variable length. char is mainly characterized by pre-Distribution of storage methods. When the Data Length of varchar changes, the page distribution of storage is affected. Char and varchar2 are a contradictory unity, and they are complementary. Varchar2 saves space than char, and is slightly less efficient than char. That is, to achieve efficiency, a certain amount of space must be sacrificed, this is what we often say in Database Design: "change space for efficiency ". Although varchar2 saves space than char, if a varchar2 column is often modified and the length of the data to be modified is different each time, this will cause "Row Migration, this causes redundant I/O, which should be avoided during database design and adjustment. In this case, it would be better to replace varchar2 with char. The difference between mysql_connect and mysql_pconnect references a friend at the exceed php club Forum's original saying: mysql_pconnect () Implementation Method in php: in fact, mysql_pconnect () itself has not done much processing, the only thing it does is not actively close the mysql connection after the php operation ends. The differences between mysql_pconnect () and mysql_connect () include: in cgi Mode, pconnect and connect are basically the same when php is run in cgi Mode, the cgi method means that every php accesses a process. After the access, the process ends and all resources are released. Apache module mode: the difference is that when php runs in the apache module mode, because apache uses a process pool, an httpd process will be put back into the process pool after it ends, this will prevent the mysql connection resource opened with pconnect from being released, so it can be reused when there is a connection request. This makes it possible for php to save time for repeatedly connecting to the database due to the use of pconnect when the concurrency traffic of apache is low, which speeds up access. This should be easy to understand. However, if you use pconnect when the number of concurrent accesses to apache is large, it may be because the mysql connection occupied by some httpd processes has not been closed, in this way, subsequent requests will never be satisfied. For example, if the maximum number of connections in mysql is set to 500, and the maximum number of concurrent connections in apache is set to 2000, it is assumed that all accesses require access to the database, and the operation time will be relatively long. When the httpd of the current 500 requests are not completed, the subsequent httd processes cannot connect to mysql (because the maximum number of connections to mysql has been reached ), mysql can be connected only when the current 500 httpd processes are completed or reused. In fact, this is also a good explanation of the xgy_p test if the operation is relatively simple, pconnect is much more efficient than connect, and is close to the speed of using the jsp connection pool. At this time, the httpd process can be reused continuously. When DB operations are complex and take a long time, because httpd will fork many concurrent processes for processing, the first httpd process will not release the db connection, making the subsequent httpd process unable to connect to the db, because the mysql connection of other httpd processes is not reused in this way, many connection times out. For example, in the first 1000 concurrent connection tests, almost all connection times out. (In turn, If jsp uses a pure db connection pool, there will be no connection failure due to reaching the mysql connection ceiling, the jsp connection pool makes it possible to wait for other connections to be used up and reused .) Therefore, when the concurrent access volume is not high, pconnect can be used to increase the access speed. However, when the concurrent access volume increases, whether to use pconnect again depends on the programmer's choice. I personally think that the connection pool is not actually used by php for mysql connections, and pconnect is only used by the apache process pool, therefore, pconnect does not greatly improve the efficiency of accessing the database when the concurrency traffic is high. At this point, php is indeed inferior to jsp. In this case, if the concurrency is large, I personally suggest using mysql_connect. The difference between include and require is as follows: The require () Performance of phpchina.cn php is similar to that of include. The difference is that for include (), the file is read and evaluated each time when include () is executed; For require (), the file is processed only once (in fact, the file content replaces the require () Statement ). This means that if the code containing one of these commands and the code that may be executed multiple times, the use of require () is more efficient. On the other hand, if different files are read each time the code is executed, or there is a loop through a set of file stacks, use include (), because you can set a variable for the file name you want to include, this variable is used when the parameter is include. During the execution of include, if an error occurs in the file included in the include statement, it will not be immediately stopped; while require will immediately terminate the program and stop executing it. Include can be used in loops; require cannot. The following is taken from ricky 1. require is unconditionally included. That is, if require is added to a process, require will be executed no matter whether the condition is true or not. This is not applicable anymore, Because require can contain files pointed to by variables, such as: if ($ a = 1) {$ file = '1. php ';} else {$ file =' 2. php ';} require ($ file); 2. When the contained file does not exist or the syntax is incorrect, require is fatal, and include is not. 3. include has a return value, but require does not (probably because such require is faster than include ). $ Login = include ('test. php'); if (! Empty ($ login) {echo "File Inclusion succeeded";} else {echo "File Inclusion failed";} There are two methods to reference a file: require and include. The two methods provide different elasticity. The use of require is as follows: require ("MyRequireFile. php ");. This function is usually placed at the beginning of the PHP program. Before the PHP program is executed, it will first read the file specified by require to make it a part of the PHP program webpage. This method can also be used to introduce common functions into webpages. Include usage methods such as include ("mydomaindefile. php ");. This function is generally placed in the Process of process control. The PHP program webpage reads the include file. In this way, you can simplify the process during program execution. The difference between isset () and empty () is that both are used to test variables, but isset () is used to test whether variables are assigned values, while empty () is to test whether a variable that has been assigned a value is null. If a variable is not assigned a value, it is allowed to be referenced in php, but there will be a notice prompt. If a variable is assigned a null value, $ foo = "" or $ foo = 0 or $ foo = false, empty ($ foo) returns true, and isset ($ foo) returns true, that is to say, assigning a null value does not cancel a variable. To cancel a variable, you can use unset ($ foo) or $ foo = NULL.

Original article address: some interesting tips in PHP. Thank you for sharing them with me.

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.