PHP 1.27 - How To Work With PHPs Configuration File

blog

When working with PHP, you might want to configure and customize certain things within the PHP. That's why PHP comes with the PHP configuration file, which is called php.ini. The location of this file will depend on a few factors like server operating system and so on. For example, if you're using XAMPP, like I am in this case, it makes it simple to check and update the php.ini file. You can just click on "Config" next to Apache, and you can find the php.ini. Open it and update whatever you need.

Just a few things to note here: the text enclosed in square brackets [] is ignored and emicolon ; indicates that it's a comment It's just comments or documentation. And then you can find the individual settings, or also called directives, in here. So if we scroll down, we can see the "short_open_tag" is set to "off," and everything above it is just comments explaining what this directive does.

There are a lot of options that can be configured, and we won't go through all of them because we don't have the time. But we can go through some of the important ones. I have the list of available directives open here in the PHP docs, and I will leave the link to this page at the end of the lessson:

The first column here is just the name of the directive. The second one is the default value of that directive. The third one is changeable, and then the fourth one is changelog. Now, I would not worry about the "changeable" column too much. This basically just indicates where this directive can be changed from. You could use functions like ini_get() and ini_set() to get and update the directives. But some of these directives cannot be updated through PHP during runtime. It can only be updated through the php.ini file. If it says "PHP_INI_SYSTEM" or "PHP_INI_PERDIR," then you cannot use ini_set() to update the directive.

Now, as mentioned, the ini_set() updates the value of the directive during the runtime, and what that means is that it will only be effective during the script execution. I will be using ini_set() throughout this lesson to set the directive values at runtime for the sake of demonstration. Even though it's fine to use ini_set() for certain use cases, in a real application, you would want to set those at the PHP.ini configuration file.

Note that once you update your php.ini, you might need to restart your Apache server for those changes to take effect. So let's go over some of the important directives.

The first one we're going to go over is the 'error_reporting'. 

So 'error_reporting' allows you to set different levels of PHP error reporting. So for example, if we open the php.ini and we search for "error_reporting," we see that it's set to "E_ALL." That means that all errors, including warnings, notices, and everything, will get reported. And as you can see, you could change that, and you could use bitwise operator to decide the level of your error reporting.

As mentioned, you could change this during the runtime. So we could do here:

<?php

var_dump(ini_get('error_reporting'));
string(5) "22527"

This is just a representation of E_ALL value. This is a constant. So if we did var_dump(E_ALL), we see that it's the same value:

<?php

var_dump(ini_get('error_reporting'));
var_dump(E_ALL);
string(5) "32767" int(32767)

For example, if we have an array that has only one value, that is at the zeroth index, if we try to access an array element that doesn't exist. We'll get a warning. But we can set it in our error reporting to ignore warnings and not display it on the screen. Though this is not recommended at all, you should always use E_ALL for development and even sometimes in production because it allows you to fix your bugs ahead of time. But for the sake of demonstration, we can do ini_set('error_reporting', 0);

Note that you can also set 'error_reporting' using the error_reporting() function, but we'll talk about that in the next lesson. 

The next item is 'display_errors', and 'display_errors' simply indicates whether you want to display errors on the screen or not. So we can say var_dump(ini_get('display_errors')); and we get the value 1:

<?php

var_dump(ini_get('error_reporting'));
string(1) "1"

And we can set it to 0. And we no longer see the errors:

<?php

var_dump(ini_get('display_errors'));
ini_set('display_errors', 0);

$array = [1];

echo $array[3];
string(1) "1"

Now, in production, you would probably set display_errors to 0 so you wouldn't show any of the sensitive information within your errors to the user. But instead, it would log your errors, and that's where the 'error_log' comes in. error_log basically indicates where the errors should be logged.

We'll talk about error handling in the next lesson. Next, we have 'post_max_size', and we haven't covered forms or posting data, but this setting basically determines how much data can be posted in a request.

Next, we have 'max_execution_time', and this just sets the maximum number of seconds the script can run before it is timed out and terminated. By default, it's set to 30 seconds. For example, if we set max_execution_time to say 3 seconds and we sleep for 5 seconds and then we have "Hello, world!" Let's see what happens:

<?php

ini_set('max_execution_time', 3);

sleep(5);

echo 'Hello World';
Fatal error: Maximum execution time of 3 seconds exceeded

Next, we have 'memory_limit', and this just sets the maximum amount of memory a script can consume. We could also set the memory_limit to -1, which removes the memory limit entirely. But I would advise against it because it's better to always find a way to optimize your code to see where the memory is being consumed. That way, you're not running into memory issues.

Let's move on to the next directive, and that is 'file_uploads'. This just enables or disables the file uploads in PHP. We haven't talked about file uploads yet, and we'll cover that later, so don't worry about it right now.

Related to file uploads, we also have 'upload_tmp_dir', and this indicates the temporary folder where the uploaded files will be stored. When doing file uploads, we also have 'upload_max_file_size'.

Another one we have is 'date.timezone', which is the default time zone that you set.

Next, we have 'include_path', and we covered this briefly when we talked about including PHP files. This specifies the list of directories where the require, include, require_once, include_once, and other functions that deal with opening files would look for files by default.

We'll talk more about the php.ini and more advanced configuration directives once we get to the security part of this course because there are some directives that you should keep disabled, and some directives, like session directives, that you should know and be aware of how they work when it comes to security.

PHP .ini directives - https://www.php.net/manual/en/ini.list.php

Share: