Lucas
Lucas

Reputation: 159

How to echo some code inside a while loop?

I have been thinking for hours but i still cant get a solution for this

Basically what i want to do is to echo a separator inside a while, it should be something like this

$num = 1;
while($num < 3){

echo 'dog';

//function to stop while
echo 'separator';
//function to continue while

echo 'cat';

$num++;
}

I want to get this output

dog
dog
dog
separator
cat
cat
cat

I dont know if i explained myself well but hope you understand. Thank you very much in advance.

Update: I know i can make this with 2 while functions but is it possible to make it using only one while function?

Upvotes: 0

Views: 204

Answers (7)

Sanjeev
Sanjeev

Reputation: 1866

definitely yes you can with one while ~function. :)

function OnlyOneWhileFunction($echoThis, $howManyTimes){
    $i = 1;
    while($i <= $howManyTimes){
        echo $echoThis."\r\n";
        $i++;
    }
}

OnlyOneWhileFunction('dog', 3);
echo 'separator';
OnlyOneWhileFunction('cat', 3);

Upvotes: 2

gmartellino
gmartellino

Reputation: 707

Here's another approach if you wanted n number of options and assuming each option is iterated over the same number of times. I'd do some cleanup but this is a quick and dirty approach:

<?php

/**
 * Loops items with separator every nth time
 * @param array list of items
 * @param integer number of iterations
 * @param string separator text
 */
function loopItemsWithSeparator(array $items, $count, $separator) {

    $items = array_reverse($items);
    while(!empty($items)) {
        $item = array_pop($items);
        for($i = 0; $i < $count; $i++) {
            echo $item . "\n";
        }

        if (count($items) > 0) {
            echo($separator . "\n");
        }
    }

}

loopItemsWithSeparator(array('dog', 'cat', 'bird'), 3, 'separator');

?>

Upvotes: 0

Marty
Marty

Reputation: 39458

Alternate solution:

$items = array_reverse(array("cat","dog"));
$output = array();

while(count($items) > 0)
{
    $item = array_pop($items);
    $output[] = implode("\n", array_fill(0, 3, $item));
}

echo implode("\nseparator\n", $output);

You can replace \n with <br> for HTML output (or use nl2br).

Upvotes: 1

Sharanya Dutta
Sharanya Dutta

Reputation: 4021

The following code should work:

$num = 0;
while($num <= 6){
    if($num < 3)         echo 'dog<br/>';
    else if($num == 3)   echo 'separator<br/>';
    else                 echo 'cat<br/>';
    $num++;
}

Upvotes: 0

Joshua Bixler
Joshua Bixler

Reputation: 531

$num = 0;
$dogs = '';
$cats = '';
$seperator = 'seperator';
while($num < 3){

$dogs .= 'dog';

$cats .= 'cat';

$num++;
}
echo $dogs . $seperator . $cats;

Save the output of each of the dogs and cats then combine at end.

Upvotes: 1

sfyn
sfyn

Reputation: 706

You would be better off using a C style for loop and a function call for this:

$recho = function($out, $limit) {
    for ($x=0; $x<$limit; $x++) {
        echo $out . "\n";
    }
}

$recho('dog',3);
echo "seperator\n";
$recho('cat',3);

Put this inside a <pre> on a webpage to get your line breaks, or replace the "\n" with <br> tags.

Upvotes: 0

kimbarcelona
kimbarcelona

Reputation: 1136

Try this:

<?php
$num = 0;
while($num < 7){
if($num < 3)
echo 'dog';
elseif($num == 3)
echo 'separator';
elseif($num>3)
echo 'cat';
echo "<br>";
$num++;
}
?>

Upvotes: 0

Related Questions