我在
PHP 对象、模式和实践书中遇到了 php
sprintf
字符串:
$txtout = "";
$pad = 4*$num;
$txtout .= sprintf( "%{$pad}s", "" );
我不确定,但我认为作者的目的是基于
$pad
构建 padding(indent),但实际上它不起作用,这段代码在语法上正确吗?
它确实可以添加空格或填充 - 只是在纯 html 中通常你不会看到空格/填充 - 尽管在
pre
标签内它会变得明显。
$num=10;
$txtout = "";
$pad = 4*$num;
$txtout .= sprintf( "%{$pad}s", "" );
/* you should see the word hello spaced out across the page */
echo '<pre>',print_r( $txtout.'hello'.$txtout.'hello',true ),'</pre>';