❮ Tham khảo Linh tinh PHP
Ví dụ
In một tin nhắn và chấm dứt tập lệnh hiện tại
$site = "https. //www. w3schools. com/";
fopen[$site,"r"]
or die["Không thể kết nối với $site"];
?>
Định nghĩa và cách sử dụng
Hàm die[] là bí danh của hàm exit[]
cú pháp
Giá trị tham số
Tham số Mô tả tin nhắnBắt buộc. Một thông báo hoặc số trạng thái để in trước khi kết thúc tập lệnh. Số trạng thái sẽ không được ghi vào đầu ra, chỉ được sử dụng làm trạng thái thoátchi tiết kỹ thuật
Giá trị trả về. Không có gìPhiên bản PHP. 4+❮ Tham khảo Linh tinh PHPHàm này trả về một giá trị khác 0 nếu cuộc gọi bị gián đoạn bởi tín hiệu. Trên Windows, giá trị này sẽ luôn là 192, là giá trị của hằng số WAIT_IO_COMPLETION trong Windows API. Trên các nền tảng khác, giá trị trả về sẽ là số giây còn lại để ngủ
You definitely need to be careful about using relative paths in after the shutdown function has been called, but the current working directory doesn't [necessarily] get changed to the web server's ServerRoot - I've tested on two different servers and they both have their CWD changed to '/' [which isn't the ServerRoot].
This demonstrates the behaviour:
function echocwd[] { echo 'cwd: ', getcwd[], "\n"; }
register_shutdown_function['echocwd'];
echocwd[] and exit;
?>
Outputs:
cwd: /path/to/my/site/docroot/test
cwd: /
NB: CLI scripts are unaffected, and keep their CWD as the directory the script was called from.
proc_open is probably a better solution for most use cases as of PHP 7.4. There is better control and platform independence. If you still want to use shell_exec[], I like to wrap it with a function that allows better control.
________số 8
public function sh_exec[string $cmd, string $outputfile = "", string $pidfile = "", bool $mergestderror = true, bool $bg = false] {
$fullcmd = $cmd;
if[strlen[$outputfile] > 0] $fullcmd .= " >> " . $outputfile;
if[$mergestderror] $fullcmd .= " 2>&1";
if[$bg] {
$fullcmd = "nohup " . $fullcmd . " &";
if[strlen[$pidfile]] $fullcmd .= " echo $! > " . $pidfile;
} else {
if[strlen[$pidfile] > 0] $fullcmd .= "; echo $$ > " . $pidfile;
}
shell_exec[$fullcmd];
}
This demonstrates the behaviour:
0
This demonstrates the behaviour:
1
This demonstrates the behaviour:
2
This demonstrates the behaviour:
3
This demonstrates the behaviour:
4
This demonstrates the behaviour:
5
This demonstrates the behaviour:
6
This demonstrates the behaviour:
7