2

Is it possible to set php.ini values from within PHP? Optimally with a limited scope?

Background: I want to force-allow short_open_tag for the duration of an include command to facilitate content writers’ jobs. The actual PHP web application uses long tags throughout (well, one at the beginning and one at the end, really) but for the content files I’d like to enable the more convenient shortcuts.

The content files are then rendered by a combination of output buffering and require, and the result of this is used to fill out a view-specific template. (I know about template engines such as Smarty but I don’t want to use them here.)

The php.ini documentation unfortunately doesn’t tell how to query/set the configuration values.

5
  • 1
    short_open_tag specifically can not be changed at runtime anymore. Commented Dec 29, 2011 at 11:42
  • @mario: as of php 5.3.0, it can. Commented Dec 29, 2011 at 11:47
  • @Cicada: Ah, ok. Got that mixed up. (It's a dead feature to me when it only works on a fifth of deployment targets.) Commented Dec 29, 2011 at 11:49
  • Wouldn't it be simpler to just change the tags (e.g. perl -e "s/<\?\s/<\?php /g;" -pi.save $(find DirectoryName -type f) ) Commented Dec 29, 2011 at 12:18
  • @symcbean Apart from the fact that it’s not quite that straightforward (what about <? when it occurs in a string?), I want to use short tags here, to help the content writers. Short tags are in general a good thing (apart from the fact that PHP, as usual, botched it up by using something that is ambiguous when used together with XML). Commented Dec 29, 2011 at 12:21

3 Answers 3

6

Would ini_set do the job ?

string ini_set ( string $varname , string $newvalue )

There's also ini_get for querying the configuration.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, thanks. Although as Mario mentions in a comment above, this unfortunately doesn’t work for short_open_tag in PHP v5.2 (which I’m unfortunately using). So I’ll resort to .htaccess instead.
2

You can dynamically set some of the PHP configuration variables using ini_set(): http://php.net/ini_set. Note that not all variables can be changed at run-time, but short_open_tag can be. See here for more details on what can and can't be changed in which situations: http://php.net/ini.core.php.

Comments

1

if you used ini_set able to control php.ini settings programaticaly

ini_set('display_errors', 1);

http://php.net/manual/en/ini.core.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.