AnnanFay
AnnanFay

Reputation: 9739

How can I change PHP ini settings from within a shell script?

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

Upvotes: 0

Views: 3075

Answers (5)

Adi
Adi

Reputation: 556

How about (mind the @ character):

<?php

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

?>

Upvotes: 0

Pascal MARTIN
Pascal MARTIN

Reputation: 400912

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.

Upvotes: 0

Gumbo
Gumbo

Reputation: 655129

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

Upvotes: 1

gnarf
gnarf

Reputation: 106322

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)

Upvotes: 0

RaYell
RaYell

Reputation: 70404

Try setting error reporting using error_reporing() function

error_reporting(0);

Upvotes: 1

Related Questions