How to Execute Command in PHP
There are two ways to run a command in PHP. Let’s consider the below:
1. Using exec
function
<?php
exec('your-command');
The exec
function also lets us get the output from the command by adding the output argument in the 2nd parameter of the function, for example:
<?php
exec('php -version 2>&1', $output);
var_dump($output);
Output result:
array (size=3)
0 => string 'PHP 7.2.1 (cli) (built: Jan 4 2018 04:29:12) ( ZTS MSVC15 (Visual C++ 2017) x86 )' (length=82)
1 => string 'Copyright (c) 1997-2017 The PHP Group' (length=37)
2 => string 'Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies' (length=61)
2. Using shell_exec
function
<?php
shell_exec('your-command');