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

Jul 29, 2016 am 08:35 AM

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.

🎜
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles