国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
[Transfer] Will php continue to execute after the browser is exited? , php continues to execute
Home Backend Development PHP Tutorial [Transfer] Will php continue to execute after the browser exits? ,php continues to execute_PHP tutorial

[Transfer] Will php continue to execute after the browser exits? ,php continues to execute_PHP tutorial

Jul 12, 2016 am 08:52 AM
php

[Transfer] Will php continue to execute after the browser is exited? , php continues to execute

Original link: http://www.cnblogs.com/yjf512/p/5362025.html

Premise: What we are talking about here is the typical lnmp structure, nginx php-fpm mode

If I have a php program that executes very slowly, even sleep() in the code, and then when the browser connects to the service, a php-fpm process will be started, but at this time, if the browser is closed, So, will the php-fpm process on the server continue to run at this time?

Today we are going to solve this problem.

The simplest experiment

The easiest way is to do an experiment. Let’s write a program: use file_put_contents to write logs before and after sleep:

<code class="hljs cs"><?<span class="hljs-function">php
<span class="hljs-title">file_put_contents(<span class="hljs-params"><span class="hljs-string">'/tmp/test.log', <span class="hljs-string">'11111' . PHP_EOL, FILE_APPEND | LOCK_EX);
sleep(<span class="hljs-number">3);
file_put_contents(<span class="hljs-string">'/tmp/test.log', <span class="hljs-string">'2222' . PHP_EOL, FILE_APPEND | LOCK_EX);</span></span></span></span></span></span></span></span></code>

The result of the actual operation is that when we close the client browser while the server is sleeping, 2222 will be written to the log.

So this means that after the browser is closed, the server-side php will still continue to run?

ignore_user_abort

Lao Wang and diogin reminded that this may be related to the ignore_user_abort function of PHP.

So I changed the code slightly to this:

<code class="hljs cs"><?<span class="hljs-function">php
<span class="hljs-title">ignore_user_abort(<span class="hljs-params"><span class="hljs-keyword">false);
file_put_contents(<span class="hljs-string">'/tmp/test.log', <span class="hljs-string">'11111' . PHP_EOL, FILE_APPEND | LOCK_EX);
sleep(<span class="hljs-number">3);
file_put_contents(<span class="hljs-string">'/tmp/test.log', <span class="hljs-string">'2222' . PHP_EOL, FILE_APPEND | LOCK_EX);</span></span></span></span></span></span></span></span></span></code>

It is found that there is no software use. No matter what value ignore_user_abort is set to, it will continue to execute.

But here is a question: What is user_abort?

The document makes it very clear about abort in cli mode. When the php script is executed and the user terminates the script, abort will be triggered. The script then determines whether to continue execution based on ignore_user_abort.

But the official document does not clearly describe abort in cgi mode. It seems that even if the client disconnects, PHP in cgi mode will not receive abort.
Does ignore_user_abort have no effect in cgi mode?

Is it a heartbeat problem?

The first thing that comes to mind is a heartbeat problem? When we disconnect the browser client, it is equivalent to disconnecting the connection without closing the client. The server needs to wait for the TCP keepalive to arrive before detecting it.

Okay, you need to troubleshoot the keepalive problem in the browser settings first.

Abandon the browser and simply write a client program: After the program connects to the http service, it sends a header and sleeps for 1 second before actively closing the connection. However, this program does not have the keepalive header of http.

The procedure is as follows:

<code class="hljs swift">package main

<span class="hljs-keyword">import "net"
<span class="hljs-keyword">import "fmt"
<span class="hljs-keyword">import "time"

<span class="hljs-function"><span class="hljs-keyword">func <span class="hljs-title">main<span class="hljs-params">() {
    conn, <span class="hljs-number">_ := net.<span class="hljs-type">Dial(<span class="hljs-string">"tcp", <span class="hljs-string">"192.168.33.10:10011")
    fmt.<span class="hljs-type">Fprintf(conn, <span class="hljs-string">"GET /index.php HTTP/1.0\r\n\r\n")
    time.<span class="hljs-type">Sleep(<span class="hljs-number">1 * time.<span class="hljs-type">Second)
    conn.<span class="hljs-type">Close()
    <span class="hljs-keyword">return
}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>

Server program:

<code class="hljs cs"><?<span class="hljs-function">php
<span class="hljs-title">ignore_user_abort(<span class="hljs-params"><span class="hljs-keyword">false);
file_put_contents(<span class="hljs-string">'/tmp/test.log', <span class="hljs-string">'11111' . PHP_EOL, FILE_APPEND | LOCK_EX);
sleep(<span class="hljs-number">3);
file_put_contents(<span class="hljs-string">'/tmp/test.log', <span class="hljs-string">'2222' . PHP_EOL, FILE_APPEND | LOCK_EX);</span></span></span></span></span></span></span></span></span></code>

I found that it is still the same. PHP will continue to execute the entire script regardless of whether ignore_user_abort is set or not. It seems that ignore_user_abort still does not take effect.

How to trigger ignore_user_abort

How to trigger ignore_user_abort? How does the server know that this socket cannot be used? Lao Wang and Diogin asked whether the server needs to actively interact with the socket to determine whether the socket can be used?

In addition, we also found that PHP provides two methods, connection_status and connection_aborted, both of which can detect the current connection status. So our logging line of code can be changed to:

<code class="hljs lisp">file_put_contents('/tmp/test.log', '<span class="hljs-number">1 connection status: ' . connection_status() . <span class="hljs-string">"abort:" . connection_aborted() . PHP_EOL, FILE_APPEND | LOCK_EX)<span class="hljs-comment">;</span></span></span></code>

According to the manual connection processing display, we can print out the current connection status.

The following is missing a program that interacts with the socket. We use echo, and remember to bring flush later to eliminate the influence of flush.

The program will be changed to:

<code class="hljs django"><span class="xml"><span class="php"><span class="hljs-meta"><?php
ignore_user_abort(<span class="hljs-keyword">true);
file_put_contents(<span class="hljs-string">'/tmp/test.log', <span class="hljs-string">'1 connection status: ' . connection_status() . <span class="hljs-string">"abort:" . connection_aborted() . PHP_EOL, FILE_APPEND | LOCK_EX);

sleep(<span class="hljs-number">3);

<span class="hljs-keyword">for($i = <span class="hljs-number">0; $i < <span class="hljs-number">10; $i++) {
        <span class="hljs-keyword">echo <span class="hljs-string">"22222";
        flush();
        sleep(<span class="hljs-number">1);
        file_put_contents(<span class="hljs-string">'/tmp/test.log', <span class="hljs-string">'2 connection status: ' . connection_status() . <span class="hljs-string">"abort:" . connection_aborted(). PHP_EOL, FILE_APPEND | LOCK_EX);
}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>

Very good, execute the client we wrote earlier. Observation log:

<code class="hljs basic"><span class="hljs-symbol">1 connection status: <span class="hljs-number">0abort:<span class="hljs-number">0
<span class="hljs-symbol">2 connection status: <span class="hljs-number">0abort:<span class="hljs-number">0
<span class="hljs-symbol">2 connection status: <span class="hljs-number">1abort:<span class="hljs-number">1
<span class="hljs-symbol">2 connection status: <span class="hljs-number">1abort:<span class="hljs-number">1
<span class="hljs-symbol">2 connection status: <span class="hljs-number">1abort:<span class="hljs-number">1
<span class="hljs-symbol">2 connection status: <span class="hljs-number">1abort:<span class="hljs-number">1
<span class="hljs-symbol">2 connection status: <span class="hljs-number">1abort:<span class="hljs-number">1
<span class="hljs-symbol">2 connection status: <span class="hljs-number">1abort:<span class="hljs-number">1
<span class="hljs-symbol">2 connection status: <span class="hljs-number">1abort:<span class="hljs-number">1
<span class="hljs-symbol">2 connection status: <span class="hljs-number">1abort:<span class="hljs-number">1
<span class="hljs-symbol">2 connection status: <span class="hljs-number">1abort:<span class="hljs-number">1</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>

Finally made abort. The log also shows that the abort status for the next few times is 1.

But there is something strange here, why is the status of the first 2 connection status still 0 (NORMAL).

RST

We use wireshark to capture packets to see the entire interaction process between the client and the server

This whole process only sends 14 packets. Let’s see that when the server sends 22222 for the first time, the client returns RST. There will be no subsequent package requests.

So I understand, the approximate interaction process between the client and the server is:

When the server sends 2222 for the first time in the loop, the client has disconnected and returns an RST, but this sending process is considered a successful request. Until the second time the server wants to perform a write operation on this socket again, this socket will not perform network transmission, and directly returns that the connection status is abort. So the above situation occurred. The first time 222 was the status was 0, and the abort appeared only the second time.

strace for verification

We can also use strace php -S XXX to verify

The strace log of the entire process is as follows:

<code class="hljs erlang">。。。
<span class="hljs-function"><span class="hljs-title">close<span class="hljs-params">(<span class="hljs-number">5)                                = 0
<span class="hljs-title">lstat<span class="hljs-params">(<span class="hljs-string">"/tmp/test.log", {st_mode=S_IFREG|<span class="hljs-number">0644, st_size=<span class="hljs-number">49873651, ...}) = 0
<span class="hljs-title">open<span class="hljs-params">(<span class="hljs-string">"/tmp/test.log", O_WRONLY|O_CREAT|O_APPEND, <span class="hljs-number">0666) = 5
<span class="hljs-title">fstat<span class="hljs-params">(<span class="hljs-number">5, {st_mode=S_IFREG|<span class="hljs-number">0644, st_size=<span class="hljs-number">49873651, ...}) = 0
<span class="hljs-title">lseek<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-number">0, SEEK_CUR)                   = 0
<span class="hljs-title">lseek<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-number">0, SEEK_CUR)                   = 0
<span class="hljs-title">flock<span class="hljs-params">(<span class="hljs-number">5, LOCK_EX)                       = 0
<span class="hljs-title">write<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-string">"1 connection status: 0abort:0\n", <span class="hljs-number">30) = 30
<span class="hljs-title">close<span class="hljs-params">(<span class="hljs-number">5)                                = 0
<span class="hljs-title">sendto<span class="hljs-params">(<span class="hljs-number">4, <span class="hljs-string">"HTTP/1.0 200 OK\r\nConnection: clo"..., <span class="hljs-number">89, <span class="hljs-number">0, NULL, <span class="hljs-number">0) = 89
<span class="hljs-title">sendto<span class="hljs-params">(<span class="hljs-number">4, <span class="hljs-string">"111111111", <span class="hljs-number">9, <span class="hljs-number">0, NULL, <span class="hljs-number">0)   = 9
<span class="hljs-title">rt_sigprocmask<span class="hljs-params">(SIG_BLOCK, [CHLD], [], <span class="hljs-number">8) = 0
<span class="hljs-title">rt_sigaction<span class="hljs-params">(SIGCHLD, NULL, {SIG_DFL, [], <span class="hljs-number">0}, <span class="hljs-number">8) = 0
<span class="hljs-title">rt_sigprocmask<span class="hljs-params">(SIG_SETMASK, [], NULL, <span class="hljs-number">8) = 0
<span class="hljs-title">nanosleep<span class="hljs-params">({<span class="hljs-number">3, <span class="hljs-number">0}, <span class="hljs-number">0x7fff60a40290)       = 0
<span class="hljs-title">sendto<span class="hljs-params">(<span class="hljs-number">4, <span class="hljs-string">"22222", <span class="hljs-number">5, <span class="hljs-number">0, NULL, <span class="hljs-number">0)       = 5
<span class="hljs-title">open<span class="hljs-params">(<span class="hljs-string">"/tmp/test.log", O_WRONLY|O_CREAT|O_APPEND, <span class="hljs-number">0666) = 5
<span class="hljs-title">fstat<span class="hljs-params">(<span class="hljs-number">5, {st_mode=S_IFREG|<span class="hljs-number">0644, st_size=<span class="hljs-number">49873681, ...}) = 0
<span class="hljs-title">lseek<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-number">0, SEEK_CUR)                   = 0
<span class="hljs-title">lseek<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-number">0, SEEK_CUR)                   = 0
<span class="hljs-title">flock<span class="hljs-params">(<span class="hljs-number">5, LOCK_EX)                       = 0
<span class="hljs-title">write<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-string">"2 connection status: 0abort:0\n", <span class="hljs-number">30) = 30
<span class="hljs-title">close<span class="hljs-params">(<span class="hljs-number">5)                                = 0
<span class="hljs-title">rt_sigprocmask<span class="hljs-params">(SIG_BLOCK, [CHLD], [], <span class="hljs-number">8) = 0
<span class="hljs-title">rt_sigaction<span class="hljs-params">(SIGCHLD, NULL, {SIG_DFL, [], <span class="hljs-number">0}, <span class="hljs-number">8) = 0
<span class="hljs-title">rt_sigprocmask<span class="hljs-params">(SIG_SETMASK, [], NULL, <span class="hljs-number">8) = 0
<span class="hljs-title">nanosleep<span class="hljs-params">({<span class="hljs-number">1, <span class="hljs-number">0}, <span class="hljs-number">0x7fff60a40290)       = 0
<span class="hljs-title">sendto<span class="hljs-params">(<span class="hljs-number">4, <span class="hljs-string">"22222", <span class="hljs-number">5, <span class="hljs-number">0, NULL, <span class="hljs-number">0)       = -1 EPIPE <span class="hljs-params">(Broken pipe)
--- SIGPIPE {<span class="hljs-title">si_signo=SIGPIPE, <span class="hljs-title">si_code=SI_USER, <span class="hljs-title">si_pid=2819, <span class="hljs-title">si_uid=0} ---
<span class="hljs-title">open<span class="hljs-params">(<span class="hljs-string">"/tmp/test.log", O_WRONLY|O_CREAT|O_APPEND, <span class="hljs-number">0666) = 5
<span class="hljs-title">fstat<span class="hljs-params">(<span class="hljs-number">5, {st_mode=S_IFREG|<span class="hljs-number">0644, st_size=<span class="hljs-number">49873711, ...}) = 0
<span class="hljs-title">lseek<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-number">0, SEEK_CUR)                   = 0
<span class="hljs-title">lseek<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-number">0, SEEK_CUR)                   = 0
<span class="hljs-title">flock<span class="hljs-params">(<span class="hljs-number">5, LOCK_EX)                       = 0
<span class="hljs-title">write<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-string">"2 connection status: 1abort:1\n", <span class="hljs-number">30) = 30
<span class="hljs-title">close<span class="hljs-params">(<span class="hljs-number">5)                                = 0
<span class="hljs-title">rt_sigprocmask<span class="hljs-params">(SIG_BLOCK, [CHLD], [], <span class="hljs-number">8) = 0
<span class="hljs-title">rt_sigaction<span class="hljs-params">(SIGCHLD, NULL, {SIG_DFL, [], <span class="hljs-number">0}, <span class="hljs-number">8) = 0
<span class="hljs-title">rt_sigprocmask<span class="hljs-params">(SIG_SETMASK, [], NULL, <span class="hljs-number">8) = 0
<span class="hljs-title">nanosleep<span class="hljs-params">({<span class="hljs-number">1, <span class="hljs-number">0}, <span class="hljs-number">0x7fff60a40290)       = 0
<span class="hljs-title">open<span class="hljs-params">(<span class="hljs-string">"/tmp/test.log", O_WRONLY|O_CREAT|O_APPEND, <span class="hljs-number">0666) = 5
<span class="hljs-title">fstat<span class="hljs-params">(<span class="hljs-number">5, {st_mode=S_IFREG|<span class="hljs-number">0644, st_size=<span class="hljs-number">49873741, ...}) = 0
<span class="hljs-title">lseek<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-number">0, SEEK_CUR)                   = 0
<span class="hljs-title">lseek<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-number">0, SEEK_CUR)                   = 0
<span class="hljs-title">flock<span class="hljs-params">(<span class="hljs-number">5, LOCK_EX)                       = 0
<span class="hljs-title">write<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-string">"2 connection status: 1abort:1\n", <span class="hljs-number">30) = 30
<span class="hljs-title">close<span class="hljs-params">(5)  
。。。</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>

Let’s take a look at the place where status changes from 0 to 1.

<code class="hljs erlang">...
<span class="hljs-function"><span class="hljs-title">sendto<span class="hljs-params">(<span class="hljs-number">4, <span class="hljs-string">"22222", <span class="hljs-number">5, <span class="hljs-number">0, NULL, <span class="hljs-number">0)       = 5
...
<span class="hljs-title">write<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-string">"2 connection status: 0abort:0\n", <span class="hljs-number">30) = 30
<span class="hljs-title">close<span class="hljs-params">(<span class="hljs-number">5)                                = 0
<span class="hljs-title">rt_sigprocmask<span class="hljs-params">(SIG_BLOCK, [CHLD], [], <span class="hljs-number">8) = 0
<span class="hljs-title">rt_sigaction<span class="hljs-params">(SIGCHLD, NULL, {SIG_DFL, [], <span class="hljs-number">0}, <span class="hljs-number">8) = 0
<span class="hljs-title">rt_sigprocmask<span class="hljs-params">(SIG_SETMASK, [], NULL, <span class="hljs-number">8) = 0
<span class="hljs-title">nanosleep<span class="hljs-params">({<span class="hljs-number">1, <span class="hljs-number">0}, <span class="hljs-number">0x7fff60a40290)       = 0
<span class="hljs-title">sendto<span class="hljs-params">(<span class="hljs-number">4, <span class="hljs-string">"22222", <span class="hljs-number">5, <span class="hljs-number">0, NULL, <span class="hljs-number">0)       = -1 EPIPE <span class="hljs-params">(Broken pipe)
--- SIGPIPE {<span class="hljs-title">si_signo=SIGPIPE, <span class="hljs-title">si_code=SI_USER, <span class="hljs-title">si_pid=2819, <span class="hljs-title">si_uid=0} ---
<span class="hljs-title">open<span class="hljs-params">(<span class="hljs-string">"/tmp/test.log", O_WRONLY|O_CREAT|O_APPEND, <span class="hljs-number">0666) = 5
<span class="hljs-title">fstat<span class="hljs-params">(<span class="hljs-number">5, {st_mode=S_IFREG|<span class="hljs-number">0644, st_size=<span class="hljs-number">49873711, ...}) = 0
<span class="hljs-title">lseek<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-number">0, SEEK_CUR)                   = 0
<span class="hljs-title">lseek<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-number">0, SEEK_CUR)                   = 0
<span class="hljs-title">flock<span class="hljs-params">(<span class="hljs-number">5, LOCK_EX)                       = 0
<span class="hljs-title">write<span class="hljs-params">(<span class="hljs-number">5, <span class="hljs-string">"2 connection status: 1abort:1\n", <span class="hljs-number">30) = 30
<span class="hljs-title">close<span class="hljs-params">(<span class="hljs-number">5)                                = 0</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>

第二次往socket中發(fā)送2222的時候顯示了Broken pipe。這就是程序告訴我們,這個socket已經(jīng)不能使用了,順便php中的connection_status就會被設(shè)置為1了。后續(xù)的寫操作也都不會再執(zhí)行了。

總結(jié)

正常情況下,如果客戶端client異常推出了,服務(wù)端的程序還是會繼續(xù)執(zhí)行,直到與IO進行了兩次交互操作。服務(wù)端發(fā)現(xiàn)客戶端已經(jīng)斷開連接,這個 時候會觸發(fā)一個user_abort,如果這個沒有設(shè)置ignore_user_abort,那么這個php-fpm的程序才會被中斷。

至此,問題結(jié)了。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1126842.htmlTechArticle[轉(zhuǎn)]瀏覽器退出之后php還會繼續(xù)執(zhí)行么?,php繼續(xù)執(zhí)行 原文鏈接:http://www.cnblogs.com/yjf512/p/5362025.html 前提:這里說的是典型的lnmp結(jié)構(gòu),ng...
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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

How to get the current session ID in PHP? How to get the current session ID in PHP? Jul 13, 2025 am 03:02 AM

The method to get the current session ID in PHP is to use the session_id() function, but you must call session_start() to successfully obtain it. 1. Call session_start() to start the session; 2. Use session_id() to read the session ID and output a string similar to abc123def456ghi789; 3. If the return is empty, check whether session_start() is missing, whether the user accesses for the first time, or whether the session is destroyed; 4. The session ID can be used for logging, security verification and cross-request communication, but security needs to be paid attention to. Make sure that the session is correctly enabled and the ID can be obtained successfully.

PHP get substring from a string PHP get substring from a string Jul 13, 2025 am 02:59 AM

To extract substrings from PHP strings, you can use the substr() function, which is syntax substr(string$string,int$start,?int$length=null), and if the length is not specified, it will be intercepted to the end; when processing multi-byte characters such as Chinese, you should use the mb_substr() function to avoid garbled code; if you need to intercept the string according to a specific separator, you can use exploit() or combine strpos() and substr() to implement it, such as extracting file name extensions or domain names.

How do you perform unit testing for php code? How do you perform unit testing for php code? Jul 13, 2025 am 02:54 AM

UnittestinginPHPinvolvesverifyingindividualcodeunitslikefunctionsormethodstocatchbugsearlyandensurereliablerefactoring.1)SetupPHPUnitviaComposer,createatestdirectory,andconfigureautoloadandphpunit.xml.2)Writetestcasesfollowingthearrange-act-assertpat

How to split a string into an array in PHP How to split a string into an array in PHP Jul 13, 2025 am 02:59 AM

In PHP, the most common method is to split the string into an array using the exploit() function. This function divides the string into multiple parts through the specified delimiter and returns an array. The syntax is exploit(separator, string, limit), where separator is the separator, string is the original string, and limit is an optional parameter to control the maximum number of segments. For example $str="apple,banana,orange";$arr=explode(",",$str); The result is ["apple","bana

JavaScript Data Types: Primitive vs Reference JavaScript Data Types: Primitive vs Reference Jul 13, 2025 am 02:43 AM

JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

Using std::chrono in C Using std::chrono in C Jul 15, 2025 am 01:30 AM

std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

How does PHP handle Environment Variables? How does PHP handle Environment Variables? Jul 14, 2025 am 03:01 AM

ToaccessenvironmentvariablesinPHP,usegetenv()orthe$_ENVsuperglobal.1.getenv('VAR_NAME')retrievesaspecificvariable.2.$_ENV['VAR_NAME']accessesvariablesifvariables_orderinphp.iniincludes"E".SetvariablesviaCLIwithVAR=valuephpscript.php,inApach

How to pass a session variable to another page in PHP? How to pass a session variable to another page in PHP? Jul 13, 2025 am 02:39 AM

In PHP, to pass a session variable to another page, the key is to start the session correctly and use the same $_SESSION key name. 1. Before using session variables for each page, it must be called session_start() and placed in the front of the script; 2. Set session variables such as $_SESSION['username']='JohnDoe' on the first page; 3. After calling session_start() on another page, access the variables through the same key name; 4. Make sure that session_start() is called on each page, avoid outputting content in advance, and check that the session storage path on the server is writable; 5. Use ses

See all articles