Skip to content
← Back to Snippets
Code

Environment Mismatch Detector

Detects configuration mismatches between expected and actual environment settings before release.

Purpose

Detects configuration mismatches between expected and actual environment settings before release.

Snippet details

ContextSystemLevelAdvancedCopy-and-paste statusMarked safe after review.

Categories

  • Security

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.
 */

/**
 * Environment Mismatch Detector.
 *
 * Purpose:
 * Detects configuration mismatches between expected and actual environment settings before release.
 *
 * @param array $expected Expected environment values.
 * @param array $actual Actual environment values.
 * @return array Mismatch report.
 */
function ogSnippetEnvironmentMismatchDetector(array $expected, array $actual): array {
	$mismatches = array();
	foreach ($expected as $key => $expected_value) {
		$actual_value = null;
		if (array_key_exists($key, $actual)) {
			$actual_value = $actual[$key];
		}
		if ($actual_value !== $expected_value) {
			$mismatches[] = array('key' => (string) $key, 'expected' => $expected_value, 'actual' => $actual_value);
		}
	}
	return $mismatches;
}

$mismatches = ogSnippetEnvironmentMismatchDetector(array('APP_ENV' => 'production', 'SSL' => 'on'), array('APP_ENV' => 'staging', 'SSL' => 'on'));
echo count($mismatches);