Some interesting little differences in PHP _ PHP Tutorial

Source: Internet
Author: User
Tags pconnect
Some interesting little differences in PHP. The difference between single quotation marks and double quotation marks: The first 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: $ value; $ value The difference between single quotation marks and double quotation marks:
The first 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; "$ value" outputs the value of the variable $ value.

Differences between char and varchar:
Char is fixed length while varchar is variable length. char is mainly characterized by pre-distribution of storage methods. when its data length changes, varchar affects the page distribution of its storage.
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.

Differences between mysql_connect and mysql_pconnect
Reference the original words of friends on the exceed php club forum:
Implementation of mysql_pconnect () in php:
In fact, mysql_pconnect () itself does not do much processing. The only thing it does is not actively close the mysql connection after the php operation ends.
Differences between mysql_pconnect () and mysql_connect:
Cgi mode:
There is basically no difference between pconnect and connect when php runs in cgi mode, because each php accesses a process and the process ends after the access is complete, all resources are released.
Apache module mode:
The difference is that when php runs as an apache module, 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 the use of pconnect when the apache concurrency traffic is low, php saves the time for repeatedly connecting to the database, and speeds up access. This should be a better understanding.
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
Assume that all accesses require access to the database, and the operation takes a long time.
When the httpd of the current 500 requests is not completed... 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 terminated 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. as a result, the mysql connection of other httpd processes is not reused. therefore, many connection times out. for example, in the first 1000 concurrent connection tests, almost all connection times out.
---
(In reverse view, 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.

Difference Between include and require
Phpchina.cn
Php's require () performance is similar to include. The difference is that for include (), the file must be 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.

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 no longer applicable because require can contain files pointed to by variables, such

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 so 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 files: 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.

Differences between isset () and empty ()
Both are used to test the variables, but isset () is used to test whether the variables are assigned values, and empty () is used to test whether a variable that has been assigned values 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.

Difference: The first is that single quotes are more efficient than double quotes, because double quotation marks are used to pre-process the content. For example: '$ value' output character $ value; "$ value" input...

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.