Skip to content

Instantly share code, notes, and snippets.

@infynyxx
Created February 8, 2012 20:27
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 infynyxx/1773160 to your computer and use it in GitHub Desktop.
Save infynyxx/1773160 to your computer and use it in GitHub Desktop.
PHP speed – empty VS isset VS === with error suppression
<?
$myvar = array();
$times = 1000000;
$start = microtime(true);
for($x=0;$x<$times;$x++){
if( @$myvar['test'] === null ) { }
}
$end = microtime(true);
$duration = $end-$start;
printf("Test null: %s \n", $duration);
$start = microtime(true);
for($x=0;$x<$times;$x++){
if( !isset( $myvar['test'] )) { }
}
$end = microtime(true);
$duration = $end-$start;
printf("Test isset: %s \n", $duration);
$start = microtime(true);
for($x=0;$x<$times;$x++){
if( empty( $myvar['test'] )) { }
}
$end = microtime(true);
$duration = $end-$start;
printf("Test empty: %s \n", $duration);
// populate
$myvar['test'] = true;
$start = microtime(true);
for($x=0;$x<$times;$x++){
if( @$myvar['test'] === null ) { }
}
$end = microtime(true);
$duration = $end-$start;
printf("Test null: %s \n", $duration);
$start = microtime(true);
for($x=0;$x<$times;$x++){
if( !isset( $myvar['test'] )) { }
}
$end = microtime(true);
$duration = $end-$start;
printf("Test isset: %s \n", $duration);
$start = microtime(true);
for($x=0;$x<$times;$x++){
if( empty( $myvar['test'] )) { }
}
$end = microtime(true);
$duration = $end-$start;
printf("Test empty: %s \n", $duration);
@AjmalPraveeN
Copy link

AjmalPraveeN commented Aug 24, 2021

A suggestion, <? ?> Avoid php short open tags.. because most won't enable short open tags.
and
Its better to have php full tag <?php ?> , It will work whether short_open_tag is enabled/disabled, its better to avoid php short tags.

❤ Just an suggestion (Got the code through search) Hope this will help others too..
Thank you.

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