Read the manual, and look at the signature:
string exec ( string $command [, array &$output [, int &$return_var ]] )
Thus:
$lastLine = exec('ls', $output, $status);
var_dump($output);
Is what you're after!
exec
indeed returns the last line of output, but it accepts 3 arguments. The second is a reference to an array (for some reason, it needn't be declared beforehand, no notices are issued here). The third is the exit status (sort of like you'd get when you do anecho $?
after a command/program has finished running on your command-line.
To quote the manual:
If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().
This means, the best use (as in get the most info) of exec
is this:
$last = exec('ls -lta', $fullList, $status);
if ($status !== 0)
{
exit('command didn\'t finish as expected: '.$status);
}
print_r($fullList);
If this still isn't making sense to you, then refer to any of the duplicate questions on this site