简体   繁体   中英

PHP loop isn't working, what am I doing wrong?

I am building a function that would receive 2 params, a string and a number. it would basically print the first letters $n of $s.

  • I need to run a loop, please don't advise other non looping methods.
  • And yes, I need to keep the loop true throughout the function, it's suppose to close when the if is met.

For some reason the loop isn't closing, even though there's a return in the if condition that is being met when $stringCounter equals $n (=10 in this example.)

function printCounter($s, $n)
{
    $stringCaller = '';
    $stringCounter = strlen($stringCaller);
        while (1) {
            $stringCaller .= $s;
            if ($stringCounter == $n) {
                return $stringCaller;
            }
        }
}

printCounter('aba', '10');

You should imove the calc of $stringCounter inside the loop otherwise this never change

    $stringCaller = '';

    while (1) {
        $stringCounter = strlen($stringCaller);
        $stringCaller .= $s;
        if ($stringCounter >= $n) {
            return $stringCaller;
        }
    }

On my oppinion, TS is searching next approach:

function printCounter($s, $n)
{
    $result = '';
    $str_lenght = strlen($s);
    if(!$str_lenght) {
        return $result;
    }
    while (true) {
        $result .= $s;
        $result_lenght = strlen($result);
        if($result_lenght/$str_lenght >= $n) {
            return $result_lenght;
        }
    }
}

echo printCounter('aba', '10');
exit;

You can fix part of the issue by updating $stringCounter inside the loop.

function printCounter($s, $n)
{
    $stringCaller = '';
    while (1) {
        $stringCaller .= $s;
        $stringCounter = strlen($stringCaller); // check strlen after each addition of $s
        if ($stringCounter == $n) {
            return $stringCaller;
        }
    }
}

However, even after you fix that issue, there will still be cases where your loop will never exit (such as the example in your question) because $stringCounter can only equal $n if $n is a multiple of strlen($s) .

For the example printCounter('aba', '10');

Iteration   $stringCounter    ($stringCounter == $n)
    1             3                   false
    2             6                   false
    3             9                   false
    4             12                  false

Obviously $stringCounter will only get farther away from $n from that point.

So to ensure that the function will exit, check for inequality instead with:

if ($stringCounter >= $n) {

If you need the function to return exactly $n characters, the function will need to be a little more complex and take a substring of $s to make up the remaining characters when $stringCounter + strlen($s) will be greater than $n , or return an $n character substring of your result like this:

function printCounter($s, $n)
{
    $result = '';
    while (1) {
        $result .= $s;
        if (strlen($result) >= $n) {
            return substr($result, 0, $n);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM