(goto list of all tests)

Look for a value

Check to see if a string is exists in a dataset of strings.

Link to the blog post about this test.

(To understand this test better I suggest you read the blog post, link above) Using 3 different methods I check to see if a certain value exists in a previous defined set of values.
I'm doing 10000 iterations.

Titlefirst valuemid valuelast valueno hitAvarageDifference
in_array 0.003428 0.003287 0.001968 0.001967 0.002662 100.00%
strpos 0.001192 0.001264 0.001001 0.001022 0.001120 42.06%
isset in array 0.000646 0.000444 0.000371 0.000328 0.000447 16.80%

The winner is without a doubt isset.

Source:

<?php
// --- Informative block
$title          "Look for a value";
$teaser         "Check to see if a string is exists in a dataset of strings.";
$description    "(To understand this test better I suggest you read the blog post, link above) Using 3 different methods I check to see if a certain value exists in a previous defined set of values.<br /> I'm doing 10000 iterations.";
$conclusion     "The winner is without a doubt isset.";
$link             "http://birk-jensen.dk/2010/12/checking-for-a-value/";

// --- Actual test
if ( isset($runTest) ) {
    
// -- Initialize the test
    
require_once(dirname(__FILE__) . "/../Bench.php");
    
$b = new Bench();
    
$stylesA = array("none""hidden""dotted""dashed""solid""double""groove""ridge""inset""outset""inherit");
    
$stylesS "none hidden dotted dashed solid double groove ridge inset outset inherit";
    
$stylesK array_flip($stylesA);

    
$checkFor = array("none""solid""inherit""nohit");
    
$it 10000;
    
    
$headers = array("first value""mid value""last value""no hit");

    
$dontSave true;
    
// Check in_array first
    
for ($cf 0$cf count($checkFor); $cf++) {
        
$res = (in_array($checkFor$stylesA) === true);
        
$b->start("in_array"$checkFor[$cf]);
        for (
$i 0$i $it$i++) {
            
$res = (in_array($checkFor$stylesA) === true);
        }
        
$b->end("in_array"$checkFor[$cf]);
        
        
$res = (strpos($stylesS$checkFor[$cf]) !== false);
        
$b->start("strpos"$checkFor[$cf]);
        for (
$i 0$i $it$i++) {
            
$res = (strpos($stylesS$checkFor[$cf]) !== false);
        }
        
$b->end("strpos"$checkFor[$cf]);
        
        
$res =  (isset($stylesK[$checkFor[$cf]]) === true);
        
$b->start("isset in array"$checkFor[$cf]);
        for (
$i 0$i $it$i++) {
            
$res =  (isset($stylesK[$checkFor[$cf]]) === true);
        }
        
$b->end("isset in array"$checkFor[$cf]);
        
$dontSave false;
    }
}