Home  >  Article  >  Backend Development  >  Some interesting differences in PHP_PHP Tutorial

Some interesting differences in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:59:30943browse

The difference between single quotes ' and double quotes ":
First of all, single quotes are more efficient than double quotes because double quotes will preprocess the content.
For example: '$value 'Output character $value; "$value" outputs the value of variable $value.

The difference between char and varchar:
char is fixed length and varchar is variable length. The main feature is that the storage method is pre-allocated. When the data length of varchar changes, it will affect the page allocation of its storage.
char and varchar2 are a contradictory unity, and the two are complementary.
varchar2. It saves space than char, but is slightly less efficient than char. That is, if you want to gain efficiency, you must sacrifice a certain amount of space. This is what we often say in database design, "Trading space for efficiency."
varchar2. Although it saves space than char, if a varchar2 column is frequently modified, and the length of the modified data is different each time, this will cause a 'row migration' (Row Migration) phenomenon, which will cause redundant I/O and is a problem for the database. It should be avoided in design and adjustment. In this case, it would be better to use char instead of varchar2.

The difference between mysql_connect and mysql_pconnect
Quote from the excel php club forum. Friend’s original words:
The implementation method of mysql_pconnect() in PHP:
In fact, mysql_pconnect() itself does not do much processing. The only thing it does is not to actively close the mysql connection after PHP is finished running.
The difference between mysql_pconnect() and mysql_connect():
In cgi mode:
When php is run in cgi mode, there is basically no difference between pconnect and connect, because cgi mode Each PHP access starts a process. After the access is completed, the process ends and all resources are released.
In apache module mode:
The difference is that when PHP is run in apache module mode, since apache is used Process pool, after an httpd process ends, it will be put back into the process pool, which means that the mysql connection resource opened with pconnect will not be released, so it can be reused when there is the next connection request.
This is When the concurrent access to Apache is not large, due to the use of pconnect, PHP saves the time of repeatedly connecting to the db, making the access speed faster. This should be easier to understand.
But when the concurrent access to Apache is large , if you use pconnect, because the mysql connections occupied by some previous httpd processes are not closed, it may be because mysql has reached the maximum connection, so that some subsequent requests will never be satisfied.
For example:
If The maximum number of mysql connections is set to 500, and the maximum number of simultaneous accesses to apache is set to 2000
Assume that all accesses will require access to the db, and the operation time will be relatively long
The current 500 httpd requests have not ended. ..The subsequent httpd processes will not be able to connect to mysql (because the maximum number of mysql connections has been reached). Only when the current 500 httpd processes end or are reused can they be connected to mysql.
In fact, this is also easy to explain. In the test of xgy_p, if the operation is relatively simple, pconnect is much more efficient than connect, and is close to the speed of the connection pool using jsp. Because at this time, the httpd process can be continuously reused.
When the DB operation is complex, When it takes a long time, because httpd will fork many concurrent processes, the httpd process generated first does not release the db connection, so that the httpd process generated later cannot connect to the db. This is because the mysql connection of other httpd processes is not reused. So A lot of connection timeouts will occur. For example, the initial 1,000 concurrent connection tests said that almost all connections timed out. This is the reason.
---
(In turn, if the jsp uses a pure db connection pool, Then there will be no problem of being unable to connect due to reaching the upper limit of mysql connection, because the jsp connection pool will allow you to wait for other connections to be used up and reused. )
Therefore, when the concurrent access volume is not high, using pconnect can be simple Improve access speed, but after the amount of concurrency increases, whether to use pconnect again depends on the programmer's choice.
In my personal opinion, PHP does not really use the connection pool for mysql connections, and pconnect does not It is just equivalent to borrowing the process pool of apache, so pconnect cannot improve the efficiency of accessing DB when the amount of concurrent access is large. At this point, php is indeed not as good as jsp.
As of now In this case, if the amount of concurrency is large, I personally recommend using 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(), the file must be read and evaluated every time when include() is executed; while for require(), the file is only processed once (in fact, the file content replaces require () statement).This means that if you have code that contains one of these instructions and code that may be executed multiple times, it is more efficient to use require(). On the other hand, if you read a different file each time the code is executed, or have a loop that iterates through a set of files, use include() because you can set a variable for the name of the file you want to include, and when the argument is Use this variable when including(). When

include is executed, if an error occurs in the file included, it will not stop immediately; while require will terminate the program immediately and no longer execute.
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 first regardless of whether the condition is true or not.
This is no longer applicable, because require can include The file pointed to by the variable is such as

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

2. require is fatal when the included file does not exist or has a syntax error. include is not

3. include has a return value, and require does not (maybe because require is faster than include)
$login = include('test.php');
if(!empty($login)){
echo "File included successfully" ;
}else{
echo "File inclusion failed";
}

There are two ways to reference files: require and include. The two methods provide different usage flexibility.

require is used as require("MyRequireFile.php");. This function is usually placed at the front of the PHP program. Before the PHP program is executed, it will first read in the file specified by require and make it a part of the PHP program web page. Commonly used functions can also be introduced into web pages in this way.

include is used as include("MyIncludeFile.php");. This function is generally placed in the processing part of flow control. The PHP program web page only reads the include file when it reads it. In this way, the process of program execution can be simplified.

The difference between isset() and empty()
Both are used to test variables, but isset() tests whether a variable has been assigned a value, while empty() tests a variable that has been assigned a value. Whether it is empty.

If a variable is not assigned a value, it is allowed to be referenced in PHP, but there will be a notice that if a variable is assigned a null value, $foo="" or $foo=0 or $foo= false, then empty($foo) returns true and isset($foo) also returns true, which means assigning a null value will not unregister a variable.

To unregister a variable, you can use unset($foo) or $foo=NULL

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317318.htmlTechArticleThe difference between single quotes ' and double quotes ": First, single quotes are more efficient than double quotes because Double quotes will preprocess the content. For example: '$value' outputs the character $value; "$value" input...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn