[Windows/Linux] PHP “exec” missing environment variable causing command not to run

In a recent project, I am finding way to display current git branch on the backend dashboard.
Without a application dependent library, executing CLI git seems to be the fastest way.
i.e. The exactly same syntax as we use in bash/cmd/powershell.

$currentBranch = exec('git rev-parse --abbrev-ref HEAD');

However, it worked in production/staging Linux machine, but not in local Windows machine.
It does work if I specify the absolute path C:\Program Files\Git\bin\git.exe, but it’s not optimal because it will be machine dependent.

After a while, I figure out the environment PATH variable is missing in Windows that cause git not being picked up.
The trick is, add this line before exec call to:

putenv('PATH=' . $_SERVER['PATH']);

WAMP Development Machine Setup Note

Install vanilla Apache 2.4, PHP 7.0, MySQL 5.7 separately
Not using XAMPP because I don’t like their structure.
And they run PHP by Apache module, which cause bug and crash (Especially on Laravel).

For my own note (For copy and paste :D)

Updated 2017-05-03: Change to fastcgi setup to prevent Apache crashing

Continue reading “WAMP Development Machine Setup Note”

Block Microsoft’s new stealth Windows 10 upgrade

Save the following text as “block-win10.reg” and double click to run it

Windows Registry Editor Version 5.00



or download the file directly:

Reboot after run. Yeah. That SHIT is gone!

Source: https://support.microsoft.com/en-us/kb/3080351