PHP has many configuration settings that determine how it behaves. The configuration settings are stored in a text file named as php.ini. PHP looks for the file php.ini when it begins and uses the settings it finds. If PHP can’t find the file, it uses a set of default settings.
All PHP settings can be changed in the php.ini file. Some settings should always be changed, and some should be changed only in specific circumstances. For example, magic quotes should always be turned off. However, a Shared Web host isn’t going to allow you access to the general php.ini file, because it controls the settings for all the users on the server, not just for your site. So, you change any PHP settings on your Web hosting account with a different procedure:
- A local php.ini file: Some Web hosts allow you to have a local php.ini file that controls: PHP’s behaviour for your Web site only. If so, you can make any needed changes in this local php.ini file.
- An .htaccess file: You can add directives to your .htaccess file that change PHP settings. Only some settings can be changed this way.
- A statement in the PHP program: You can add a statement to a PHP program that changes the settings for that program. The new settings only apply to the program it’s in, and the old setting is still in place after the script ends. Only some settings can be changed this way.
Notice that only some settings can be changed in an .htaccess file or in a PHP program. You can find a table that shows where PHP settings can be changed at https://www.php.net/manual/en/ini.list.php. One column in the table is labelled Changeable.
The codes in that column define where the setting can be changed, as follows:
- PHP_INI_ALL: Can be changed anywhere
- PHP_INI_PERDIR: Can be changed in the .htaccess file
- PHP_INI_USER: Can be changed temporarily in the PHP program
- PHP_INI_SYSTEM: Can be changed only in the php.ini file
1. Changing Settings in php.ini
You can change all your PHP settings in the php.ini file. You can always edit your own php.ini file with a text editor on your computer. If your Web host allows you a local php.ini on your Web site, you can edit that also with an editor.
In the general php.ini file Because php.ini is a text file, you can edit it with any text editor. Follow these steps to do so:
- Locate the php.ini file that is currently in effect. As you all knows, you can see that path to this file in the output from the phpinfo() statement in a PHP program.
- Open php.ini in your favourite text editor such as Sublime or Vscode or Notepad++.
- Scroll down to the setting you want to change. In this example, We’re turning magic quotes off.
- Change On to Off.
- Save the changed php.ini file.
- Restart your Apache Web Server.
Any changes you make to php.ini do not go into effect until you restart Apache.
In a php.ini file on your Web site If you’re allowed a local php.ini file by your Web host, it doesn’t need to contain all the settings that are in the general php.ini file. It needs to contain only the settings that you want to change.
2. Changing Settings with an .htaccess File
The file named .htaccess is an Apache configuration file. Apache reads the file and performs certain tasks based on the directives in the file. You can add directives to the .htaccess file that tell Apache to change the configuration settings of PHP. As already mentioned, not all PHP settings can be changed in this file.
The settings in the .htaccess file apply to all programs in the directory where the .htaccess file resides and in its subdirectories. You can have more than one .htaccess file — files with different directives in different directories.
You may or may not already have an .htaccess file on your Web site. Some Web hosts use .htaccess files on user accounts for their own settings. If there is an .htaccess already on your Web site, be careful when you edit it.
It may contain directives that are essential to the correct functioning of your Web site. It’s best not to have a copy of the .htaccess file in your development environment. The file on your Web site might contain directives that are required on your Web site but cause problems on your local development site, even including disabling your local site.
The directives you use in your .htaccess file to change PHP settings are
- php_flag: Used to turn settings on or off.
- php_value: Used to set a value for a setting.
To turn off magic quotes, as shown above, use the following directive in the .htaccess file:
php_flag magic_quotes_gpc Off
If you need to set a value for a setting, you use the other directive, such as
php_value post_max_size = 10M
To change a setting, you just need to edit the .htaccess file to add the new setting. If your Web host provides the ability to edit a file on your Web site, you can edit it there.
If you need to edit the .htaccess file on your development site, download the current .htaccess file, add the directive to change the setting, and upload the changed file back to your Web site. Be sure to delete the .htaccess file on your development site after you have uploaded it to your Web site.
3. Changing Settings with PHP Statements
You can change some PHP settings temporarily with a PHP statement in a program. The setting is changed only while the program is running. When the program finishes, the PHP setting reverts to its previous setting.
PHP provides a statement that changes a setting. The general format is
For this example, we’re turning off errors.
PHP also has some specific statements for certain settings. You can use these instead of the general ini_set statement. For example, PHP provides a statement for setting the type of errors you want to display.
It’s quite common to want to change which type of errors you display. You might want to use a statement that displays more error messages during development, but you want to display fewer error messages when the Web site goes public. You would rather display error messages in a log file than display them to the general public.
To change the type of errors displayed, temporarily, you use the following statement at the beginning of your program:
error_reporting( E_ALL );
This statement causes all error messages to be displayed while this program is running, but not for any other program.
Another statement provided by PHP to change a setting is
This statement sets the amount of time a program can run before PHP decides that something has gone wrong and ends the program.