Function name: system()
Applicable versions: All PHP versions
Usage: The system() function is used to execute operating system commands and output the results to standard output. It is similar to the exec() function that outputs the result directly after executing the command, but instead returns the output of the command, it sends it directly to the output stream.
Syntax: system(string $command, int &$return_var = null): string|false
parameter:
Example:
// 示例1:执行命令并输出结果system('ls'); // 示例2:执行命令并获取返回值$return_var = null; $output = system('php -v', $return_var); echo "返回值: " . $return_var . "\n"; echo "输出结果: " . $output;
In Example 1, the system() function executes a simple command "ls" and outputs the result directly to standard output.
In Example 2, the system() function executes the command "php -v" and stores the return value in the $return_var variable, and the output is saved in the $output variable. We can then output the return value and output result separately.
It should be noted that since the system() function outputs the result directly to the standard output, you need to be careful when executing commands, ensuring that only trusted commands are executed and avoiding user-provided input to the function directly to prevent security issues.