Defining INI Entries for PHP Scripts

Annika Backstrom
in misc, on 15 November 2012. It is tagged #Programming, #PHP, #PHPUnit, #PHP_CodeSniffer, and #scripting.

Any semiregular PHP user knows how to pass INI settings to the command line interface:

$ php -d apc.enable_cli=1 script.php

But what about scripts that are executable, with a shebang and chmod +x? Here's a simple debug script we'll call apc-status.php:


echo json_encode($argv), PHP_EOL;
echo "apc.enable_cli = ", ini_get('apc.enable_cli'), PHP_EOL;

Normal invocation gives us this:

$ ./apc-status.php hello
apc.enable_cli = 0

But the -d flag doesn't work as we might expect:

$ ./apc-status.php -d apc.enable_cli=1 hello
apc.enable_cli = 0

We can see that -d apc.enable_cli=1 has made it thorugh to the script, rather than being absorbed and handled by the PHP interpreter. Stepping back a bit, this makes sense: the interpreter is defined in the shebang #!/usr/bin/php. All flags after the script name are handed off to the script.

By calling the php binary directly, we can define our custom settings:

$ php -d apc.enable_cli=1 apc-status.php hello
apc.enable_cli = 1

Alternately, you could modify the shebang:

#!/usr/bin/php -d apc.enable_cli=1<?php

Use one of these techniques if you need to pass ini settings to wrapper scripts like PHP_CodeSniffer's phpcs runner, or phpunit.