Home > Backend Development > PHP Tutorial > What does hard work pay off? Some interesting differences in PHP

What does hard work pay off? Some interesting differences in PHP

WBOY
Release: 2016-07-29 08:35:28
Original
1469 people have browsed it

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" Output the value of variable $value.
The difference between char and varchar:
char is fixed length and varchar is variable length. The main feature of char is that the storage method is pre-allocated. When the data length of varchar changes, it will affect the page it is stored in. Allocation.
char and varchar2 are a contradictory unity, and the two are complementary.
varchar2 saves space than char, and is slightly less efficient than char. That is, to gain efficiency, a certain amount of space must be sacrificed. , this is what we often say in database design, "Trading space for efficiency". Although varchar2 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 The 'Row Migration' phenomenon, which causes redundant I/O, should be avoided in database design and adjustment. In this case, it would be better to use char instead of varchar2.
The difference between mysql_connect and mysql_pconnect.
Quoting the original words of a friend on the excel php club forum:
The implementation 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 after php is finished running. mysql connection.
mysql_pconnect() and the difference between mysql_connect():
cgi mode:
When php is run in cgi mode, there is basically no difference between pconnect and connect, because in cgi mode, each php access starts a process , after the access is completed, the process will end and all resources will be released.
In apache module mode:
The difference is that when php is run in apache module mode, since apache uses a process pool, an httpd process will be put back after it ends Process pool, this also prevents the mysql connection resource opened with pconnect from being released, so it can be reused when there is the next connection request.
This allows when the concurrent access of 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 volume of apache is large, if you use pconnect, it will be due to the mysql connection occupied by some previous httpd processes. Without close, it may be that mysql has reached the maximum number of connections, 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 of apache is set to 2000
Assumption All accesses will require access to the db, and the operation time will be relatively long.
When the current 500 httpd requests are not completed... the subsequent httd process will not be able to connect to mysql (because the maximum number of mysql connections has been reached). Only The current 500 httpd processes must be terminated or reused before they can be connected to mysql.
In fact, this also explains very well that if the operation in the xgy_p test is relatively simple, pconnect is much more efficient than connect, and it is as fast as using the jsp connection pool Closer. Because the httpd process can be continuously reused at this time.
When the DB operation is complex and takes a long time, httpd will fork many concurrent processes, and the httpd process generated first will not release the db connection, causing the subsequent The generated httpd process cannot connect to the db. This is because the mysql connections of other httpd processes are not reused. Therefore, many connection timeouts will occur. For example, the initial 1000 concurrent connection tests said that almost all connection timeouts were due to this reason.
- --
(Looking back, if jsp uses a pure db connection pool, there will be no problem of being unable to connect due to reaching the mysql connection limit, because the jsp connection pool will wait for other connections to be used and reused. . )
So when the number of concurrent visits is not high, using pconnect can simply improve the access speed, but after the amount of concurrency increases, it depends on the programmer's choice whether to use pconnect again.
In my personal opinion, php is now The connection to mysql does not really use the connection pool, pconnect 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.
In the current situation, 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 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().
include When executing, 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 an unconditional inclusion, 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 files pointed to by variables such as
if($ a = 1){
$file = '1.php';
}else{
$file = '2.php';
}
require($file);
2, when the included file does not exist or has a syntax error require is fatal, include is not
3, include has a return value, but require does not (maybe because require is faster than include)
$login = include('test.php');
if(!empty($login )){
echo "File included successfully";
}else{
echo "File included failed";
}
There are two ways to reference files: require and include. The two methods provide different usage flexibility.
require is used like 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 whether a variable that has been assigned a value is empty.
If a variable is referenced in PHP without being assigned a value, it is allowed, but there will be a notice prompt. If a variable is assigned an empty value, $foo="" or $foo=0 or $foo=false, then empty( $foo) returns true, 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

The above has introduced some interesting differences between PHP and PHP, including the content of PHP tutorial. I hope it will be helpful to friends who are interested in PHP tutorials.

🎜
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template