开发者

How can I change php.ini by PHP?

I want to do the equivalent to the following line in 开发者_StackOverflow社区file php.ini, but from PHP.

short_open_tag = On

Is it possible?

I tried this:

<?php
    if (!ini_get('short_open_tag')) {
        ini_set('short_open_tag', 'On');
    }
    $a = 1;
?>

<?=$a;?>

which outputs <?=$a;?>, so it's not working.


Yes, ini_set() is what you want.

An example:

if (!ini_get('short_open_tag')) {
    ini_set('short_open_tag', 'on');
}


If you are using PHP 5.3, short_open_tag is no longer an option.

Description of core php.ini directives

Short tags have been deprecated as of PHP 5.3 and may be removed in PHP 6.0.


If you want to change it during a session and forget about it later, use ini_get() and ini_set(). If you want to actually modify php.ini programmatically, you can parse the ini file using parse_ini_file(), change your options and rewrite back to disk. See here for more.

Or you can write your own string replacement routine using the normal opening of a file, preg_replace(), etc.


Although you can use ini_set, be careful (quoted from the PHP documentation):

Not all the available options can be changed using ini_set(). There is a list of all available options in the appendix.

If you are changing options, like magic_quotes and short_open_tags, that's OK. But if you are going to change safe_mode, enable_dl, etc., the function will fail silently.

Many of the options specified above as examples are obsolete/removed security options in former versions of PHP. Consult the documentation if the behavior of ini_set is unexpected (e.g., does not work)


Please edit the php.ini file (just remove the ; and restart your Apache server):

Replace

;short_open_tag = On

with

short_open_tag = On 

Now it will work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜