Skip to content

Instantly share code, notes, and snippets.

@aurimasniekis
Created January 27, 2016 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aurimasniekis/adb6b826089c08d9c380 to your computer and use it in GitHub Desktop.
Save aurimasniekis/adb6b826089c08d9c380 to your computer and use it in GitHub Desktop.
PHP Checking if array key exists and getting value
<?php
$given = array_flip(range(1, 99999));
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
if (false !== isset($given['3434'])) {
$value = $given['3434'];
}
}
echo "First: " . (microtime(true) - $start) . PHP_EOL;
$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
$value = $given['8912'] ?? null;
if (null !== $value) {
}
}
echo "Second: " . (microtime(true) - $start) . PHP_EOL;
First: 0.02646803855896
Second: 0.0024199485778809
@getbyid
Copy link

getbyid commented May 7, 2019

First:  $i < 100000;  // 1e5
Second:  $i < 10000;  // 1e4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment