PHP sprintf使用问题

PHP sprintf使用问题,php,printf,Php,Printf,我在php对象、模式和实践手册中遇到了一个phpsprintf字符串: 我不确定,但我认为作者的目标是基于$pad构建填充(缩进),但实际上它不起作用,这段代码在语法上正确吗?添加空格或填充确实有效-只是在普通html中通常看不到空格/填充-尽管在pre标记中它会变得明显 $num=10; $txtout = ""; $pad = 4*$num; $txtout .= sprintf( "%{$pad}s", "" ); /* you should see the word hello spa

我在php对象、模式和实践手册中遇到了一个php
sprintf
字符串:


我不确定,但我认为作者的目标是基于
$pad
构建填充(缩进),但实际上它不起作用,这段代码在语法上正确吗?

添加空格或填充确实有效-只是在普通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>';
$num=10;
$txtout=“”;
$pad=4*$num;
$txtout.=sprintf(“%{$pad}s”,”);
/*你应该会看到hello这个词在这一页上被隔开了*/
echo“”,print_r($txtout.hello.$txtout.hello',true),“”;

看起来确实像是胡说八道——也许用stru\u repeat要容易得多。我假设有一个编号分配给
$num
?它正在添加空格though@RamRaider是
$num
是一个整数,从代码的另一部分获取谢谢,它确实有效,但是将
print\u r
第二个参数更改为
true
有什么必要?使用
true
作为第二个参数意味着
print\u r
返回值,而不是将其打印到屏幕上。。可以用一个基本的
echo
语句做同样的事情,但这就是我的测试设置。是的,我知道,但我从未见过使用
连接字符串,就像我一直使用的
一样,它是否符合使用
的条件,
而不是
来连接多个字符串?是的-您可以像在echo语句中那样将内容串在一起
$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>';