Current Location: Home> Function Categories> system

system

Execute external programs and display output
Name:system
Category:Program execution
Programming Language:php
One-line Description:Execute operating system commands and output the results to standard output

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:

  • $command: The command string to be executed.
  • &$return_var (optional): A reference variable that stores the return value of the command. If this parameter is provided, it will be assigned as the return value of the command after the function is executed.
  • Return value: If the command is executed successfully, the output of the command will be returned; if there is a failure or an error, the return false.

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.

Similar Functions
Popular Articles