diff --git a/admin/cli/install.php b/admin/cli/install.php
index 0b6c5cac392..28e9cb8eafa 100644
--- a/admin/cli/install.php
+++ b/admin/cli/install.php
@@ -146,14 +146,9 @@ define('PHPUNIT_TEST', false);
define('IGNORE_COMPONENT_CACHE', true);
-// Check that PHP is of a sufficient version
-if (version_compare(phpversion(), "5.6.5") < 0) {
- $phpversion = phpversion();
- // do NOT localise - lang strings would not work here and we CAN NOT move it after installib
- fwrite(STDERR, "Moodle 3.2 or later requires at least PHP 5.6.5 (currently using version $phpversion).\n");
- fwrite(STDERR, "Please upgrade your server software or install older Moodle version.\n");
- exit(1);
-}
+// Check that PHP is of a sufficient version as soon as possible.
+require_once(__DIR__.'/../../lib/phpminimumversionlib.php');
+moodle_require_minimum_php_version();
// set up configuration
global $CFG;
diff --git a/admin/cli/install_database.php b/admin/cli/install_database.php
index f41263ed9e5..cede6cb05dd 100644
--- a/admin/cli/install_database.php
+++ b/admin/cli/install_database.php
@@ -62,14 +62,9 @@ Example:
\$sudo -u www-data /usr/bin/php admin/cli/install_database.php --lang=cs --adminpass=soMePass123 --agree-license
";
-// Check that PHP is of a sufficient version
-if (version_compare(phpversion(), "5.6.5") < 0) {
- $phpversion = phpversion();
- // do NOT localise - lang strings would not work here and we CAN NOT move it after installib
- fwrite(STDERR, "Moodle 3.2 or later requires at least PHP 5.6.5 (currently using version $phpversion).\n");
- fwrite(STDERR, "Please upgrade your server software or install older Moodle version.\n");
- exit(1);
-}
+// Check that PHP is of a sufficient version as soon as possible.
+require_once(__DIR__.'/../../lib/phpminimumversionlib.php');
+moodle_require_minimum_php_version();
// Nothing to do if config.php does not exist
$configfile = __DIR__.'/../../config.php';
diff --git a/admin/index.php b/admin/index.php
index 6cf7f45e91b..d0da26ba679 100644
--- a/admin/index.php
+++ b/admin/index.php
@@ -29,14 +29,9 @@ if (!file_exists('../config.php')) {
die();
}
-// Check that PHP is of a sufficient version as soon as possible
-if (version_compare(phpversion(), '5.6.5') < 0) {
- $phpversion = phpversion();
- // do NOT localise - lang strings would not work here and we CAN NOT move it to later place
- echo "Moodle 3.2 or later requires at least PHP 5.6.5 (currently using version $phpversion).
";
- echo "Please upgrade your server software or install older Moodle version.";
- die();
-}
+// Check that PHP is of a sufficient version as soon as possible.
+require_once(__DIR__.'/../lib/phpminimumversionlib.php');
+moodle_require_minimum_php_version();
// make sure iconv is available and actually works
if (!function_exists('iconv')) {
diff --git a/install.php b/install.php
index 5d1e8fcd835..9793d99c130 100644
--- a/install.php
+++ b/install.php
@@ -61,14 +61,9 @@ date_default_timezone_set(@date_default_timezone_get());
@error_reporting(E_ALL);
@ini_set('display_errors', '1');
-// Check that PHP is of a sufficient version.
-if (version_compare(phpversion(), '5.6.5') < 0) {
- $phpversion = phpversion();
- // do NOT localise - lang strings would not work here and we CAN not move it after installib
- echo "Moodle 3.2 or later requires at least PHP 5.6.5 (currently using version $phpversion).
";
- echo "Please upgrade your server software or install older Moodle version.";
- die;
-}
+// Check that PHP is of a sufficient version as soon as possible.
+require_once(__DIR__.'/../../lib/phpminimumversionlib.php');
+moodle_require_minimum_php_version();
// make sure iconv is available and actually works
if (!function_exists('iconv')) {
diff --git a/lib/phpminimumversionlib.php b/lib/phpminimumversionlib.php
new file mode 100644
index 00000000000..38284efb3ca
--- /dev/null
+++ b/lib/phpminimumversionlib.php
@@ -0,0 +1,76 @@
+.
+
+// MOODLE_INTERNAL check intentionally missing to allow this to be used more widely!
+
+/**
+ * A set of PHP-compatible convenience functions to check Moodle minimum PHP version in
+ * a unified place.
+ *
+ * PLEASE NOTE: This file is made to be both php-version compatible and without requirement on
+ * any moodle functions or installation so it can be used in installer or incompatible PHP versions.
+ *
+ * @package core
+ * @copyright 2017 Dan Poltawski
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+/**
+ * Require our minimum php version or halt execution if requirement not met.
+ * @return void Execution is halted if version is not met.
+ */
+function moodle_require_minimum_php_version() {
+ // PLEASE NOTE THIS FUNCTION MUST BE COMPATIBLE WITH OLD UNSUPPORTED VERSIONS OF PHP!
+ moodle_minimum_php_version_is_met(true);
+}
+
+/**
+ * Tests the current PHP version against Moodle's minimum requirement. When requirement
+ * is not met returns false or halts execution depending $haltexecution param.
+ *
+ * @param bool $haltexecution Should execution be halted when requirement not met? Defaults to false.
+ * @return bool returns true if requirement is met (false if not)
+ */
+function moodle_minimum_php_version_is_met($haltexecution = false) {
+ // PLEASE NOTE THIS FUNCTION MUST BE COMPATIBLE WITH OLD UNSUPPORTED VERSIONS OF PHP.
+ // Do not use modern php features or Moodle convenience functions (e.g. localised strings).
+
+ $minimumversion = '5.6.5';
+ $moodlerequirementchanged = '3.2';
+
+ if (version_compare(PHP_VERSION, $minimumversion) < 0) {
+ if ($haltexecution) {
+ $error = "Moodle ${moodlerequirementchanged} or later requires at least PHP ${minimumversion} "
+ . "(currently using version " . PHP_VERSION .").\n"
+ . "Some servers may have multiple PHP versions installed, are you using the correct executable?\n";
+
+ // Our CLI scripts define CLI_SCRIPT before running this test, so make use of
+ // to send error on STDERR.
+ if (defined('CLI_SCRIPT') && defined('STDERR')) {
+ fwrite(STDERR, $error);
+ } else {
+ echo $error;
+ }
+ exit(1);
+ } else {
+ return false;
+ }
+ }
+ return true;
+}
+
+// DO NOT ADD EXTRA FUNCTIONS TO THIS FILE!!
+// This file must be functioning on all versions of PHP, extra functions belong elsewhere.