What are some common reasons for the exec function not working as expected in a PHP script, even though it works in the console?

The most common reasons for the exec function not working as expected in a PHP script, even though it works in the console, are related to permission issues, environment variables, and the use of relative paths. To solve this problem, make sure that the user running the PHP script has the necessary permissions to execute the command and access the required resources. Additionally, set the full path to the command and any input/output files to avoid issues with relative paths.

<?php
// Example of using the full path to the command and input/output files
$output = [];
$command = '/usr/bin/ls -l';
exec($command, $output);
print_r($output);
?>