clusterBuddy clusterBuddy - 1 year ago 54
PHP Question

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
    is met.

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

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

printCounter('aba', '10');

Answer Source

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');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download