填充到动态长度的空白在打印时不显示任何内容

问题描述 投票:0回答:1

我在

PHP 对象、模式和实践
书中遇到了 php sprintf 字符串:

$txtout = "";
$pad = 4*$num;
$txtout .= sprintf( "%{$pad}s", "" );

我不确定,但我认为作者的目的是基于

$pad
构建 padding(indent),但实际上它不起作用,这段代码在语法上正确吗?

php printf padding
1个回答
3
投票

它确实可以添加空格或填充 - 只是在纯 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>';
© www.soinside.com 2019 - 2024. All rights reserved.