为什么我不能向成员函数发送字符串而只发送一个数字?

时间:2010-04-30 08:49:07

标签: php

我正在创建自己的函数来搜索某些内容的总数。但它没有正常工作。 function getNumberOfCounts获取$ fromIndex但不获取searchWord

public function getNumberOfCounts( $searchWord, $fromIndex )
{       
    $index = $fromIndex;
    $counter = 0;

    while( $index <= $endPos )
    {
        $index++;

        $pos = strpos( $this->text, $searchWord, ($index+1) );

        if( $pos > $index )
        {
            $counter++;

            $index = $pos;
        }
        else
            break;

    }

    return $counter;
}

public function searchDemo()
{
    $startPos = 11; // ex

    echo "<br /> count= " . $this->getNumberOfCounts( "Lorem", $startPos );
}

它们都属于同一类c。

编辑:我知道有一些缺失信息,但如果我尝试在getNumberOfCounts的第一行打印$ searchWord,则不会输出任何内容。

2 个答案:

答案 0 :(得分:2)

首先从第11位开始,这将大于“Lorem”的总长度。其次,在你的while循环中,你正在运行startpos&lt; endpos。 $ endpos尚未分配值,因此它甚至没有进入循环。

当我在它的时候,你在循环开始时递增索引(可能不需要)。通常情况下索引在循环结束时递增,因此您可以使用索引“索引到”数组而无需移位。

答案 1 :(得分:2)

使用

时,您可能会遇到更少的问题和更好的代码