echo string by value of variable

Hi, I wonder if I can echo a string by the value of a variable.
Basically I need to echo $str the amount of times declared in the variable $cat5.
So if the value of $cat5 is 3, I want the image to be echoed three times.
How can I do this?
Am I on the right track?

[php]<?php
$str = ‘’;
$mycats = get_categories (‘include=4’);
$cat5 = $mycats[0]->category_count;
echo ($cat5)*$str;
?>[/php]

Thanks in advance!

Nobody has a simple answer to this?

This code is not correct:
[php]echo ($cat5)*$str;[/php]

PHP will just interpret $str as a number, and result will be number too.

You can do simple this:
[php]for($i=1;$i<=$cat5;$i++) echo $str;[/php]

Or, use function str_repeat():
[php]echo str_repeat($str,$cat5);[/php]

Wauw, thanks!
I tried that road yesterday about twenty times - but couldn’t figure out how to get it done. Thank you for showing me how to do this!

Sponsor our Newsletter | Privacy Policy | Terms of Service