Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 311 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  // MOODLE_INTERNAL check intentionally missing to allow this to be used more widely!
  18  
  19  /**
  20   * A set of PHP-compatible convenience functions to check Moodle minimum PHP version in
  21   * a unified place.
  22   *
  23   * PLEASE NOTE: This file is made to be both php-version compatible and without requirement on
  24   * any moodle functions or installation so it can be used in installer or incompatible PHP versions.
  25   *
  26   * @package    core
  27   * @copyright  2017 Dan Poltawski <dan@moodle.com>
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  
  31  /**
  32   * Require our minimum php version or halt execution if requirement not met.
  33   * @return void Execution is halted if version is not met.
  34   */
  35  function moodle_require_minimum_php_version() {
  36      // PLEASE NOTE THIS FUNCTION MUST BE COMPATIBLE WITH OLD UNSUPPORTED VERSIONS OF PHP!
  37      moodle_minimum_php_version_is_met(true);
  38  }
  39  
  40  /**
  41   * Tests the current PHP version against Moodle's minimum requirement. When requirement
  42   * is not met returns false or halts execution depending $haltexecution param.
  43   *
  44   * @param bool $haltexecution Should execution be halted when requirement not met? Defaults to false.
  45   * @return bool returns true if requirement is met (false if not)
  46   */
  47  function moodle_minimum_php_version_is_met($haltexecution = false) {
  48      // PLEASE NOTE THIS FUNCTION MUST BE COMPATIBLE WITH OLD UNSUPPORTED VERSIONS OF PHP.
  49      // Do not use modern php features or Moodle convenience functions (e.g. localised strings).
  50  
  51      $minimumversion = '7.1.0';
  52      $moodlerequirementchanged = '3.7';
  53  
  54      if (version_compare(PHP_VERSION, $minimumversion) < 0) {
  55          if ($haltexecution) {
  56              $error = "Moodle $moodlerequirementchanged} or later requires at least PHP $minimumversion} "
  57                  . "(currently using version " . PHP_VERSION .").\n"
  58                  . "Some servers may have multiple PHP versions installed, are you using the correct executable?\n";
  59  
  60              // Our CLI scripts define CLI_SCRIPT before running this test, so make use of
  61              // to send error on STDERR.
  62              if (defined('CLI_SCRIPT') && defined('STDERR')) {
  63                  fwrite(STDERR, $error);
  64              } else {
  65                  echo $error;
  66              }
  67              exit(1);
  68          } else {
  69              return false;
  70          }
  71      }
  72      return true;
  73  }
  74  
  75  // DO NOT ADD EXTRA FUNCTIONS TO THIS FILE!!
  76  // This file must be functioning on all versions of PHP, extra functions belong elsewhere.