user198729
user198729

Reputation: 63626

How can I change php.ini by PHP?

I want to do the equivalent to the following line in 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.

Upvotes: 3

Views: 3397

Answers (5)

Jimmie Lin
Jimmie Lin

Reputation: 2215

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)

Upvotes: 1

Banty Roy
Banty Roy

Reputation: 967

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.

Upvotes: 0

danielrsmith
danielrsmith

Reputation: 4060

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.

Upvotes: 3

ghostdog74
ghostdog74

Reputation: 342273

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.

Upvotes: 1

Jay Zeng
Jay Zeng

Reputation: 1421

Yes, ini_set() is what you want.

An example:

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

Upvotes: 3

Related Questions