Performance comparison of PHP 5.2, 5.3, 5.4, 5.5, 5.6, 7.0

How to code optimally and efficiently?

It is a common problem for developers that they don't regularly expand their knowledge about optimal and efficient ways of programming. 
Below you can find a comparison of specific code fragments for various PHP versions. The data clearly presents what is worth using and what is best to avoid.

Strings concenation (Number of repetitions: 10000000)

 PHP version

5.2.17

5.3.29

5.4.43

5.5.28

5.6.12

7.0.11

'Hello ' . $tttt 4.357 7.356 3.209 3.171 3.272 1.728
"Hello $tttt" 4.329 7.888 3.605 3.284 3.412 1.590
"Hello {$tttt} " 4.276 7.593 3.642 3.538 3.305 1.457
'Hello ' . $tttt . ' -- ' . $tttt 7.468 13.52 6.271 6.264 6.133 3.801
"Hello $tttt -- $tttt" 6.752 12.896 6.014 5.765 5.597 2.832
'Hello ' . $tttt . ' -- ' . $tttt . ' -- ' . $tttt . ' -- ' . $tttt 13.869 25.731 12.313 12.381 11.851 7.819
"Hello $tttt -- $tttt -- $tttt -- $tttt" 12.028 22.618 11.241 10.365 10.570 5.017

Comparison (Number of repetitions: 10000000)

  PHP version

5.2.17

5.3.29

5.4.43

5.5.28

5.6.12

7.0.11

'111' == 111 2.334 3.969 1.959 1.947 2.005 1.224
'111' === 111 1.492 2.541 1.01 1.046 0.993 0.516
'111' != 111 2.331 4.045 1.94 1.997 2.018 1.189
'111' !== 111 1.517 2.58 0.987 1.004 0.995 0.514
'false' == false 1.74 2.904 1.158 1.155 1.209 0.681
'false' === false 1.511 2.469 0.983 0.991 1.000 0.519
'false' != false 1.72 2.867 1.159 1.155 1.185 0.677
'false' !== false 1.476 2.506 0.987 1.08 1.026 0.53
'1' == 0 2.279 3.613 1.731 1.71 1.751 1.093
'1' === 0 1.495 2.477 0.987 0.987 1.047 0.519
'1' != 0 2.370 3.655 1.727 1.708 1.758 1.141
'1' !== 0 1.489 2.592 0.989 1.081 0.997 0.523
if($d) 1.277 1.946 0.771 0.782 0.77 0.248
if($d !== false) 1.452 2.399 0.964 1.048 0.962 0.534
if(!$d) 1.248 1.945 0.771 0.781 0.769 0.254
if($b) 1.296 1.944 0.771 0.787 0.769 0.249
if($b !== false) 1.485 2.402 0.968 0.972 0.962 0.549
if(!$b) 1.248 1.945 0.772 0.781 0.763 0.253
if($b > 0) 1.725 2.500 0.96 0.973 0.979 0.529
if($b != 0) 1.731 2.448 0.972 0.968 0.962 0.500
if($b !== 0)  1.477 2.464 0.984 0.99 0.978 0.559
if(0 < $b) 1.713 2.519 0.960 0.968 0.957 0.505
if(0 != $b) 1.742 2.601 0.960 0.972 0.960 0.508
if(0 !== $b) 1.478 2.549 1.013 0.985 1.015 0.541
if(isset($b))  1.761 2.411 1.015 1.019 1.081 0.512
if(empty($b)) 1.801 2.490 1.039 1.088 1.035 0.553
if(!empty($b)) 1.735 2.487 1.04 1.054 1.036 0.549
if([]) 1.301 1.946 0.774 0.814 0.767 0.250
if(['yyy']) 1.249 2.038 0.773 0.874 0.771 0.248
if(false) 1.162 1.759 0.664 0.672 0.657 0.249
if(0) 1.142 1.74 0.662 0.664 0.658 0.249
if(null) 1.107 1.889 0.665 0.673 0.658 0.250

isset (Number of repetitions: 10000000)

 PHP version

5.2.17

5.3.29

5.4.43

5.5.28

5.6.12

7.0.11

if(isset(5555)) 1.583 2.860 1.164 1.148 1.116 0.724
if(isset($GLOBALS["foo"])) 2.47 4.984 1.492 1.475 1.385 0.867
if(isset($_SESSION["foo"])) 2.01 3.989 1.362 1.304 1.255 0.757
if(isset(5555)) 2.135 4.232 1.394 1.383 1.347 0.621
if(isset($GLOBALS["foo"])) 3.339 7.33 1.933 1.872 1.794 0.891
if(isset($_SESSION["foo"])) 7.513 14.095 6.504 5.779 6.811 4.740

isset vs key_exist (Number of repetitions: 10000000)

$array1 = array ( 'a' => 'aa', 'b' => 'bb', 'c' => 'cc', 'd' => 'dd', )
  PHP version

5.2.17

5.3.29

5.4.43

5.5.28

5.6.12

7.0.11

if(isset($array1['c'])) 1.898 3.145 1.169 1.147 1.186 0.626
if(empty($array1['c'])) 1.916 3.275 1.144 1.144 1.147 0.625
if(array_key_exists('c', $array1)) 4.043 11.875 3.690 3.691 3.375 1.112
$array2 = array ( 0 => 'aa', 1 => 'bb', 2 => 'cc', 3 => 'dd', )
  PHP version

5.2.17

5.3.29

5.4.43

5.5.28

5.6.12

7.0.11

if(isset($array2[3])) 1.543 2.87 1.085 1.073 1.071 0.682
if(empty($array2[3])) 1.761 2.85 1.174 1.124 1.102 0.697
if(array_key_exists(3, $array2)) 3.5 10.268 3.324 3.304 3.149 1.055 
  • piątek, 11 sierpień 2017