[lang-ref] ( dict_key_not_found ) ( php )

<?php
#[WithoutErrorHandler]
public function testDictKeyNotFound(): void
{
	// returns null, but warning occurs
	$d = [ 'A' => 1, 'B' => 2, 'C' => 3 ];

	set_error_handler(
		static function (int $errno, string $errstr) use (&$captured): bool {
			$captured = [$errno, $errstr];
			return true; // mark as handled (for test)
		},
		E_WARNING
	);

	try {
		$value = $d['D']; // <- here
	} finally {
		restore_error_handler();
	}

	$this->assertSame(null, $value); // return value is null
	$this->assertArrayNotHasKey('D', $d); // ensure new key is not created 

	$this->assertSame(E_WARNING, $captured[0]); // warning occurred
	$this->assertStringContainsString('Undefined array key', $captured[1]); // warning message
}