PHP

Overriding PHP Settings

There are a few ways to override global PHP (php.ini) settings with local settings. For example you can use a .htaccess file or local php.ini file depending on what options exist for your site hosting. If your site uses php-fpm you use a .user.ini file instead.

Another way to do it, is to override the settings in config.php. This has the benefit of being part of the standard moodle code configuration setup. All you need to do is use the ini_set() function.

For example, to increase the max_execution_time from 30s to 60s you can do this in config.php:

ini_set('max_execution_time', 60);

The best place for a change like this is towards the end of the config.php, with a starting comment indicating it is custom configuration.

Check that your configuration is in place by navigating to:

Site administration > Server > PHP info

Getting PHP Info for your site

There is a handy link under:

Site adminstration > Server > PHP Info
/admin/phpinfo.php

That will return the standard PHP Info phpinfo() results for your site.

Very handy for checking things like installed modules and core parameters if you are overriding them ( e.g. error_log, max_execution_time, post_max_size, upload_max_size).

If you need to override any php options/values, be sure to check out the article on Overriding PHP Settings through config.php.

PHP Syntax Check

Ever had to make a change to a PHP file on the fly only to discover you made a syntax error? E.g. config.php in Moodle?

There’s an easy way to make sure you don’t have any syntax errors in your changes:

$ php -l config.php

Any syntax errors are reported on the screen.

NOTE: the check won't tell you what the error is, just that there is one e.g. Errors parsing config.php

If there are no errors you'll see:

No syntax errors detected in config.php

PHP get key and value in loop

A cool little trick with foreach loops in PHP is you can get both the key and the value of associative array like so:

foreach ($array as $key => $value) {
    echo "$key=$value"; 
}

Very handy if you need to check the key of the value you are working with.

Learn PHP

PHP: The Right Way

"PHP: The Right Way is an easy-to-read, quick reference for PHP popular coding standards, links to authoritative tutorials around the Web and what the contributors consider to be best practices at the present time."

Clean Code PHP

"Software engineering principles, from Robert C. Martin's book Clean Code, adapted for PHP. This is not a style guide. It's a guide to producing readable, reusable, and refactorable software in PHP."

PHP Session Save Path

You can find the session.save_path through php info or using the command line:

php -i | grep session
session.save_path => /var/lib/php/session
By default if there is no value, it will use a default e.g. /var/lib/php/session but this depends on the operating system.

Once you have that, you clear PHP sessions by going into that path and clearing out the relevant session files.