Skip to content
← Back to Functions
Code

Php Compatibility Feature Check

Checks whether planned code features are supported by the configured PHP version and extension set.

Function signature

ogCheckPhpCompatibilityFeatures(target_php_version, required_features = array(), available_extensions = array())

Categories

  • System Health

Parameters

target_php_versionTarget PHP version string to compare against planned feature requirements.required_featuresPHP features, functions, or syntax markers required by planned code.available_extensionsExtensions available in the target runtime inventory.

Return value

Public-safe status string returned by the function for explicit controller branching or logging.

  • success
  • message
  • data

Compatibility

Existing function name, slug, path, and call order preserved; advertised metadata corrected to the actual source behavior.

Minimum PHP version: 7.4

Security notes

Use caller-owned allowlists and context-specific escaping; validate admin actions, export fields, privacy plans, cache keys, templates, settings, routes, and ecommerce policies before production use.

Code

<?php

/*
 * Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
 * Free for personal and internal use. Paid project use requires visible credit
 * to Jeffery L. Paris. Corporate use requires a paid license fee unless a
 * separate written license states otherwise.
 */

/**
 * Checks whether a target PHP version and extension list supports required features.
 *
 * The function returns a report only. It does not change server settings.
 *
 * @param string $target_php_version PHP version to check.
 * @param array $required_features Required language/runtime feature keys.
 * @param array $available_extensions Available extension names.
 * @return array Compatibility report.
 */
function ogCheckPhpCompatibilityFeatures($target_php_version, $required_features = array(), $available_extensions = array()) {
	$result = array(
		'success' => false,
		'message' => '',
		'data' => array()
	);

	$target_php_version = trim((string)$target_php_version);
	if (empty($target_php_version)) {
		$result['message'] = 'Target PHP version is required.';
		return $result;
	}
	if (!is_array($required_features)) {
		$required_features = array();
	}
	if (!is_array($available_extensions)) {
		$available_extensions = array();
	}

	$feature_versions = array(
		'random_bytes' => '7.0.0',
		'password_hash' => '5.5.0',
		'json_throw_on_error' => '7.3.0',
		'typed_properties' => '7.4.0',
		'match_expression' => '8.0.0',
		'constructor_promotion' => '8.0.0',
		'readonly_properties' => '8.1.0',
		'enums' => '8.1.0'
	);

	$missing_features = array();
	$supported_features = array();
	foreach ($required_features as $feature) {
		$feature = trim((string)$feature);
		if (empty($feature)) {
			continue;
		}
		if (!empty($feature_versions[$feature])) {
			if (version_compare($target_php_version, $feature_versions[$feature], '>=')) {
				$supported_features[] = $feature;
			} else {
				$missing_features[] = $feature . ' requires PHP ' . $feature_versions[$feature] . ' or newer';
			}
		} else {
			$supported_features[] = $feature;
		}
	}

	$required_extensions = array('mysqli', 'json', 'mbstring', 'openssl');
	$available_lookup = array();
	foreach ($available_extensions as $extension) {
		$available_lookup[strtolower((string)$extension)] = true;
	}

	$missing_extensions = array();
	foreach ($required_extensions as $extension) {
		if (empty($available_lookup[strtolower($extension)])) {
			$missing_extensions[] = $extension;
		}
	}

	$passes = empty($missing_features) && empty($missing_extensions);
	$result['success'] = true;
	if ($passes) {
		$result['message'] = 'PHP compatibility check passed.';
	} else {
		$result['message'] = 'PHP compatibility check found issues.';
	}
	$result['data'] = array(
		'target_php_version' => $target_php_version,
		'passes' => $passes,
		'supported_features' => $supported_features,
		'missing_features' => $missing_features,
		'missing_extensions' => $missing_extensions
	);

	return $result;
}