Php: best way to assign 10 times the same character (space) to a variable

We have

$symb="_";

$num=10;

We want it to $ten_symbsbe exactly. "__________"; // ten symbols "_". What is the fastest and / or best way to assign ten "_" to $ten_symbs?

+3
source share
4 answers

str_repeat () :

$symbol = '_';
$num    = 10;
echo str_repeat($symbol, $num);
+10
source

You can use str_repeat like:

$ten_symbs = str_repeat($symb, $num);

You can also do:

$ten_symbs = str_pad('',$num,$symb);

But the fist parameter is cleaner.

+3
source
+2
source
$ten_symbs = '';
for($i=0;$i<$num;$i++) {
   $ten_symbs .= $symb;
}
0
source

All Articles