首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用strpos检查字符串中是否存在

使用strpos检查字符串中是否存在
EN

Stack Overflow用户
提问于 2016-01-15 01:20:18
回答 5查看 2.1K关注 0票数 2

当开发人员使用strpos检查子字符串是否存在时,我只见过开发人员使用严格比较:

代码语言:javascript
运行
复制
if (strpos($haystack,$needle) !== false) {

}

今天我突然想到可以使用is_numeric ins。

代码语言:javascript
运行
复制
if (is_numeric(strpos($haystack,$needle))) {

}

是否有一个人会使用一个而另一个(特别是在这个用例中)的原因?

考虑一下,strpos的目的是返回子字符串的位置。只有当它不存在时,它才会返回假的。这个职位是个数字。因此,is_numeric很有资格被认为是语义上的。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2019-02-16 10:32:58

我发现这个问题搜索的问题几乎相同,除了我的想法是使用is_int( )而不是is_numeric( )。我无法真正理解发布的基准,所以我做了自己的。简而言之,is_numeric( )的头发比!== false慢,is_int( )的头发更快。就我个人而言,我发现它比!== false更易读,而不是更少,所以我可能会切换到它。

这是在php v7.1.23下完成的。

结果:

代码语言:javascript
运行
复制
0.7900s for '!== false' with hits
4.5137s for '!== false' with misses

0.9297s for 'is_numeric' with hits
4.7509s for 'is_numeric' with misses

0.6391s for 'is_int' with hits
4.4862s for 'is_int' with misses

根据守则:

代码语言:javascript
运行
复制
$n_times = 10000000;
$haystack_hit = "  Certificate Name: example.com";
$heystack_miss = "Found the following certs:";
$needle = "Certificate Name:";

$start = microtime(true);
for($i = 0; $i < $n_times; $i++)
    {
    if (strpos($haystack_hit,$needle) !== false)
        { }
    }
$end = microtime(true) - $start;
echo "\n" . round($end,4) . "s for '!== false' with hits\n";

$start = microtime(true);
for($i = 0; $i < $n_times; $i++)
    {
    if (strpos($haystack_miss,$needle) !== false)
        { }
    }
$end = microtime(true) - $start;
echo round($end,4) . "s for '!== false' with misses\n\n";

// -----

$start = microtime(true);
for($i = 0; $i < $n_times; $i++)
    {
    if ( is_numeric(strpos($haystack_hit,$needle)) )
        { }
    }
$end = microtime(true) - $start;
echo round($end,4) . "s for 'is_numeric' with hits\n";

$start = microtime(true);
for($i = 0; $i < $n_times; $i++)
    {
    if ( is_numeric(strpos($haystack_miss,$needle)) )
        { }
    }
$end = microtime(true) - $start;
echo round($end,4) . "s for 'is_numeric' with misses\n\n";

// -----

$start = microtime(true);
for($i = 0; $i < $n_times; $i++)
    {
    if ( is_int(strpos($haystack_hit,$needle)) )
        { }
    }
$end = microtime(true) - $start;
echo round($end,4) . "s for 'is_int' with hits\n";

$start = microtime(true);
for($i = 0; $i < $n_times; $i++)
    {
    if ( is_int(strpos($haystack_miss,$needle)) )
        { }
    }
$end = microtime(true) - $start;
echo round($end,4) . "s for 'is_int' with misses\n";
票数 2
EN

Stack Overflow用户

发布于 2016-01-15 02:06:10

我创造了一个基准。Case check by compare with false value总是比check by is_numeric快。

代码语言:javascript
运行
复制
// Init a big string array

for($i = 0; $i < 10000; $i++){
$str[] = 'a'.rand().'b'.rand();
}

// Case comparing with false value
$time1 = microtime(true);
foreach($str as $st){
$res[] = strpos($st,rand(0, count(array('b', 'c')) - 1 )) !== false;
}
$time2 = microtime(true);

echo $time2-$time1.'<br/>';

// Case 'is_numeric'
$time3 = microtime(true);
foreach($str as $st){
$res[] = is_numeric(strpos($st,rand(0, count(array('b', 'c')) - 1 )));
}
$time4 = microtime(true);
echo $time4-$time3;


//Time 1: 
//0.018877029418945 
//0.020556926727295

//Time 2:
//0.016352891921997
//0.016934871673584

//Time 3:
//0.0121009349823
//0.01330304145813

//Time 4:
//0.017507076263428
//0.01904296875
票数 3
EN

Stack Overflow用户

发布于 2016-01-15 01:26:54

由于strpos返回整数或布尔值false,所以可以使用is_numeric

问题是:

更重要的是,使用autodocumented/self-descriptive,或将返回值与布尔值进行比较,惯用的is_numeric是什么?与布尔假相比,IMO要直观得多:

代码语言:javascript
运行
复制
$string = 'new object';
$found = strpos($string,'new');

echo (is_numeric($found)) ? 'found' : 'not found';
echo "\n";
# much better
echo ($found !== false)   ? 'found' : 'not found';
echo "\n";

此外,strpos(...) !== false被过度使用,因为它是PHP文档所建议的。因此,它已成为惯例。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34802829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档