Some interesting little differences in PHP _php tips

Source: Internet
Author: User
Tags pconnect
the difference between single quotes ' and double quotes ':
The first is that single quotes are more efficient than double quotes, because double quotes will preprocess the content.
For example: ' $value ' output character $value; The value of the "$value" Output variable $value.

the difference between char and varchar:
Char is fixed-length and varchar is longer, and Char's main feature is storage-mode varchar, which affects the page allocations it stores when its data length changes.
Char and VARCHAR2 are a pair of contradictory unity, the two are complementary relations.
VARCHAR2 saves more space than Char, and is slightly less efficient than char, which means that if you want to be efficient, you have to sacrifice a certain amount of space, which is what we often say in database design, ' space for efficiency '.
VARCHAR2 saves space than Char, but if a varchar2 column is often modified and the length of each modified data varies, this causes a ' row migration ' (row migration) phenomenon, which creates redundant I/O, which is avoided in database design and tuning. , it would be better to use char instead of VARCHAR2 in this case.

the difference between mysql_connect and Mysql_pconnect
Quote from a friend at the Exceed PHP Club forum:
How to implement Mysql_pconnect () in PHP:
In fact, Mysql_pconnect () itself did not do too much processing, it only does not actively close the MySQL connection after the end of the PHP run.
Mysql_pconnect () at the same time the difference between mysql_connect ():
CGI mode:
Pconnect and connect are basically indistinguishable when PHP is run as CGI, because the CGI method is a process that every PHP accesses, the process ends after the visit, and the resources are released.
Apache Module Mode:
The difference is that when PHP is running in the Apache module, because Apache has a process pool, a httpd process is put back into the process pool, which allows the MySQL connection resource that is opened with Pconnect to be freed, so that the next connection request can be reused.
This makes the Apache concurrent access is not large, because of the use of Pconnect, PHP saves the time to repeatedly connect DB, so that the speed of access. This should be better understood.
However, when the Apache concurrent access is large, if the use of pconnect, because some of the previous httpd processes occupy the MySQL connection does not close, it may be because MySQL has reached the maximum of the next, so that some of the subsequent requests will never be satisfied.
For example:
If the maximum number of MySQL connections is set to 500, and Apache's maximum simultaneous access is set to 2000
Suppose all accesses require access to DB, and the operation time is longer
The current 500 requests httpd are not finished ... Subsequent HTTD processes are unable to connect to MySQL (since the maximum number of MySQL connections has been reached). Only the current 500 httpd processes end or are reused to connect to the MySQL.
Well, that's a good one, too. The xgy_p test, if the operation is relatively simple, pconnect than connect efficiency is much higher, and with the use of JSP connection pool speed is relatively close. Because this time the httpd process can continue to reuse.
And when the DB operation is complex, time-consuming, because httpd will fork many concurrent process processing, and the first generation of httpd process does not release the DB connection, so that the resulting httpd process can not connect to db. Because this does not duplicate the MySQL connection of other httpd processes. This results in a lot of connection timeouts, like the first 1000 concurrent connection tests that say almost all connection timeouts are the reason.
---
(Back to see if the JSP with a pure DB connection pool, there will not be due to the maximum MySQL connection is not connected to the problem, because the JSP connection pool will be able to wait for other connections to use the complete and reuse.)
Therefore, when the concurrent traffic is not high, the use of pconnect can simply improve access speed, but after the increase in concurrency, whether or not to use the pconnect depends on the programmer's Choice.
Personally, I think that the PHP connection to MySQL is not really used to connect the pool, Pconnect is just the equivalent of borrowing the process of Apache pool use, so in the concurrent access to a large amount of time pconnect does not improve access to DB efficiency. At this point. PHP does not compare with JSP.
In this case, if the concurrent volume is large, I personally suggest that the best use of mysql_connect.

the difference between include and require
The following is taken from phpchina.cn
PHP's require () performance is similar to include (). The difference is that, for include (), files are read and evaluated every time the include () is executed, whereas for require () the file is processed only once (in fact, the contents of the file replace the require () statement). This means that if you have code that contains one of these directives and code that is likely to execute multiple times, then using require () is a high efficiency. On the other hand, use include if you are reading different files each time you execute your code, or if you have a loop over a set of file iterations, because you can set a variable for the file name you want to include, and use that variable when the parameter is included ().

Include in the execution, if the include in the file error, will not immediately stop, and require will immediately terminate the program, no longer down execution.
Include can be used in loops; require not.

The following is taken from Ricky
1,require is the unconditional inclusion that is, if a process joins the require, whether the condition is established or not, it will be executed first require
This is no longer applicable, because require can contain variables pointing to files such as

if ($a = 1) {
$file = ' 1.php ';
}else{
$file = ' 2.php ';
}
Require ($file);

2, contains the file does not exist or the syntax is wrong when require is fatal, include is not

3,include has a return value, and require does not (probably because it is so require faster than include)
$login = include (' test.php ');
if (!empty ($login)) {
echo "file contains success";
}else{
echo "file contains failure";
}

There are two ways to refer to a file: Require and include. Two ways to provide different flexibility of use.

Require use methods such as require ("myrequirefile.php"); This function is usually placed at the top of the PHP program, PHP program before executing, will first read into the require designated to introduce the file, so that it becomes part of the PHP Program Web page. A commonly used function can be introduced into a Web page in this way.

Include use methods such as include ("myincludefile.php");. This function is typically placed in the processing section of the Process Control. The PHP Program page reads the included file when it is read. This way, you can simplify the process of executing a program.

The difference between isset () and Empty ()
Both are used for test variables, but Isset () is the test variable is assigned, and empty () is to test whether a variable that has already been assigned is empty.

If a variable is not assigned to the reference in PHP is allowed, but there will be notice hint, if a variable is assigned null value, $foo = "" or $foo=0 or $foo =false, then empty ($foo) return True, Isset ($foo) also return True, This means that the null value does not unregister a variable.

To unregister a variable, you can use Unset ($foo) or $foo=null

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.