views:

442

answers:

5

How can I set ini settings within a PHP script?

php.ini isn't being read and ini_set doesn't work.

Test File:

#!usr/local/bin/php -q
<?php

// display_errors is set to false in /etc/php.ini

// This shouldn't display any errors
$test = $foo;

// We can also try
ini_set('display_errors', false);

// This definitely shouldn't display an error
$test = $fum;

?>

Output:

PHP Notice:  Undefined variable: foo in /data/home/___/test.php on line 7
PHP Notice:  Undefined variable: fum in /data/home/___/test.php on line 13
+1  A: 

Try setting error reporting using error_reporing() function

error_reporting(0);
RaYell
A: 

No idea why ini_set isn't working, but the location of the php.ini file should be shown in a phpinfo(); call - make sure your using the right php.ini (and if not - you may want to symlink the two together)

gnarf
A: 

The -c option allows to specify the php.ini file that should be used.

Gumbo
A: 

In addition to the -c option, which allows to specify which php.ini file should be used (as Gumbo already stated -- my answer is just a follow up to his one), there is also the possibility to use the -d switch, which allows to define one configuration directive (without having to create a file) :

$ php --help
  ...
  -d foo[=bar]     Define INI entry foo with value 'bar'
  ...


For instance, considering you have a script that only contains this :

<?php
var_dump(ini_get('memory_limit'));
die;

You can call it this way :

$ php ./temp.php
string(4) "128M"

And also this way, redefining one configuration directive :

$ php -d memory_limit=27M ./temp.php
string(3) "27M"

This is really useful when you want to redefine a configuration option for just one script : you don't have to create a new file just for that.

Pascal MARTIN
A: 

How about (mind the @ character):

<?php

...
// This definitely shouldn't display an error
@$test = $fum;
...

?>

Ch4m3l3on
Pure evilness! http://stackoverflow.com/questions/136899/suppress-error-with-operator-in-php/960288#960288
Annan
I agree, but I think it's less evil than overriding the entire php.ini file.
Ch4m3l3on