PHP Configuration | PHP Tutorial - Learn with VOKS
Back Next

PHP Configuration


PHP can be configured using:

  1. php.ini file – The main configuration file for PHP.
  2. phpinfo() function – Displays current PHP configuration.
  3. Web server integration – Apache, Nginx, or IIS may have additional PHP settings.


Finding php.ini

  • Windows (XAMPP/WAMP): C:\xampp\php\php.ini
  • Linux (LAMP): /etc/php/8.1/apache2/php.ini (version may vary)
  • Mac (MAMP): /Applications/MAMP/bin/php/php7.x.x/conf/php.ini

You can find the loaded php.ini file using PHP:

<?php
phpinfo();
?>
  • Open the page in a browser.
  • Look for "Loaded Configuration File" – this shows the path to the active php.ini.


Important PHP Settings

1️⃣ Display Errors

display_errors = On    ; Show errors in browser (useful for development)
display_startup_errors = On
error_reporting = E_ALL
  • On during development, Off in production for security.


2️⃣ Maximum File Upload Size

upload_max_filesize = 10M
post_max_size = 20M
  • upload_max_filesize → Maximum single file size
  • post_max_size → Maximum size for entire form submission


3️⃣ Timezone Setting

date.timezone = "Asia/Kolkata"
  • Prevents errors when using date functions.
  • Replace "Asia/Kolkata" with your timezone (see PHP manual for full list).


4️⃣ Maximum Execution Time

max_execution_time = 30   ; seconds
  • Limits script execution time to avoid hanging.


5️⃣ Memory Limit

memory_limit = 128M
  • Maximum memory a PHP script can use.


6️⃣ Extensions

Enable extensions by removing ; before the line in php.ini:

extension=mysqli
extension=curl


Checking PHP Configuration

Using phpinfo()

<?php
phpinfo();
?>
  • Shows PHP version, extensions, server info, and all config values.

Using ini_get() and ini_set()

<?php
// Get current value
echo ini_get('max_execution_time'); // 30

// Change value at runtime
ini_set('max_execution_time', 60);
echo ini_get('max_execution_time'); // 60
?>
Note: Changes with ini_set() last only for the current script.


Changing PHP Configuration

  • Edit php.ini – Permanent change
  • .htaccess file (Apache only) – Temporary change for a directory:
php_value upload_max_filesize 20M
php_value post_max_size 25M
  • Runtime with ini_set() – Temporary for a script


PHP Configuration Best Practices

  • Development vs Production:
  • display_errors = On for dev
  • display_errors = Off for live site
  • Security:
  • Disable dangerous functions: exec, shell_exec, etc.
  • Set proper file_uploads and open_basedir.
  • Performance:
  • Adjust memory_limit and max_execution_time for heavy scripts.
  • Enable caching extensions like OPcache.


Practice Example: Check PHP Configuration

<?php
echo "<h2>PHP Configuration</h2>";

// PHP Version
echo "PHP Version: " . phpversion() . "<br>";

// Max Execution Time
echo "Max Execution Time: " . ini_get('max_execution_time') . " seconds<br>";

// Memory Limit
echo "Memory Limit: " . ini_get('memory_limit') . "<br>";

// Check if mysqli extension is loaded
if(extension_loaded('mysqli')){
    echo "MySQLi extension is enabled.";
} else {
    echo "MySQLi extension is not enabled.";
}
?>


This script displays:

  • PHP version
  • Maximum execution time
  • Memory limit
  • Whether MySQLi extension is enabled


Key Points:

  • php.ini is the main configuration file for PHP.
  • Use phpinfo() to check current settings.
  • Important settings: display_errors, upload_max_filesize, memory_limit, timezone.
  • Extensions provide additional functionality, enable them as needed.
  • Use ini_set() or .htaccess for script-specific adjustments.


Example Code:
<br />
<b>Deprecated</b>:  htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in <b>/home/voksinst/tutorials.voksinstitute.com/admin/topics.php</b> on line <b>265</b><br />
PHP
Introduction PHP Configuration PHP CRUD (Create, Read, Update, Delete) PHP Error Handling PHP Form Submission Login System PHP Comments
All Courses
Advance AI Bootstrap C C++ Computer Vision Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel Figma HTML Java Script Machine Learning MySQLi Node JS PHP Power Bi Python Python for AI Python for Analysis React React Native SEO SMM SQL