Does anyone know why when I execute this :
#!/usr/bin/php
<?php
$eff_theo = array_fill(0, 10, 100);
for ($i = 0; $i < 10; ++$i)
echo printf("%5d", $eff_theo[$i]).' ';
echo PHP_EOL;
?>
I get this :
1005 1005 1005 1005 1005 1005 ...
You're mixing echo
and printf
.
printf
returns the length of the formatted string, so your echo
call is printing out '5'.
Try removing the echo
and try it again....
printf returns the length of the string you have made, choose either
echo sprintf(...)
or just
printf(...)
with no echo
printf is a function that outputs your given text to standard output and returns the length of the outputted string.
echo is a language construct that is used to output data. Ultimately you are echoing the return value of the printf
statement after printf
outputs its value.
Either use echo
or printf
, but not both at the same time.