views:

129

answers:

6

I wonder what is slower or faster:

if( @$myvar['test'] === null ) { .. }

or:

if( !isset( $myvar['test'] )) { .. }

Also wondering if you suppress a warning or notice with @, will it make the evaluation slower?

Thanks for your answer!

PS: It is not about the difference, i know that isset checks if a element is set and not if it is empty or not. But in my case is only important to know if it is empty.

A: 

Only one way to find out for sure - test it.

Visage
-1 This isn't an answer. The test results (above), are.
Jon B
+2  A: 

Suppressing warnings with @ does slow down things. isset() should be the way to go here.

Fanis
+4  A: 

Generally the use of @ does create an overhead in the event of an error condition, so I'd expect it to be slower... and I'd say that using that syntax is less intuitive. Don't try to micro-optimise at the expense of readability

Mark Baker
A: 

As an aside, I remember reading some tip that claims that strict testing with === is actually marginally quicker than ==

Nev Stokes
+2  A: 
<?
$myvar = array();

$start = microtime(true);
for($x=0;$x<100000;$x++){

    if( @$myvar['test'] === null ) { }

}
$end = microtime(true);
$duration = $end-$start;
printf("Test 1: %s \n", $duration);


$start = microtime(true);
for($x=0;$x<100000;$x++){

    if( !isset( $myvar['test'] )) {  }

}
$end = microtime(true);
$duration = $end-$start;
printf("Test 2: %s \n", $duration);

// populate 
$myvar['test'] = true;

$start = microtime(true);
for($x=0;$x<100000;$x++){

    if( @$myvar['test'] === null ) { }

}
$end = microtime(true);
$duration = $end-$start;
printf("Test 3: %s \n", $duration);


$start = microtime(true);
for($x=0;$x<100000;$x++){

    if( !isset( $myvar['test'] )) {  }

}
$end = microtime(true);
$duration = $end-$start;
printf("Test 4: %s \n", $duration);

Result:
Test 1: 0.18865299224854
Test 2: 0.012698173522949
Test 3: 0.11134600639343
Test 4: 0.015975952148438

Ollie
+1 You must accept this answer. It is a plus for the entire PHP community.
hopeseekr
Thank you so much for the answer. I accept this as answer. isset is faster.
Erwinus
If you accept, you should really mark the answer as accepted.
Ollie
A: 

in PHP it doesn't matter

Col. Shrapnel
not true as you can see above
Erwinus
@Erwinus in a way. When I see such a nonsense, it drives me kinda crazy. But it's ok. thanks for your concern.
Col. Shrapnel