我试图了解更多关于PHP函数sprintf(),但php.net没有帮助我,因为我仍然困惑,为什么要使用它?

看看下面的例子。

为什么用这个:

$output = sprintf("Here is the result: %s for this date %s", $result, $date);

当这样做是一样的,更容易写IMO:

$output = 'Here is the result: ' .$result. ' for this date ' .$date;

我是不是遗漏了什么?


当前回答

在循环中使用sprintf时必须小心:

$a = 'Anton';
$b = 'Bert';
$c = 'Corni';
$d = 'Dora';
$e = 'Emiel';
$f = 'Falk';
$loops = 10000000;

$time = microtime(true);

for ($i = 0; $i < $loops; $i++)
{
    $test = $a . $b . $c . $d . $e . $f;
}

$concatTime = microtime(true) - $time;

$time = microtime(true);

for ($i = 0; $i < $loops; $i++)
{
    $test = "$a $b $c $d $e $f";
}

$concat2Time = microtime(true) - $time;

$time = microtime(true);

for ($i = 0; $i < $loops; $i++)
{
    $test = sprintf('%s %s %s %s %s %s', $a, $b, $c, $d, $e, $f);
}

$sprintfTime = microtime(true) - $time;

echo 'Loops: ' . $loops . '<br>';
echo '\'$a . $b . $c . $d . $e . $f\'' . ' needs ' . $concatTime  . 's<br>';
echo '"$a $b $c $d $e $f"' . ' needs ' . $concat2Time  . 's<br>';
echo 'sprintf(\'%s %s %s %s %s %s\', $a, $b, $c, $d, $e, $f)' . ' needs ' . $sprintfTime  . 's<br>';

这会导致以下时间(在我的本地机器上使用PHP 7.2):

循环:10000000

“一美元。b美元。$ c。美元d。$ e。$f'需要1.4507689476013s

“$a $b $c $d $e $f”需要1.9958319664001s

sprintf (' % s % s % s % s % s % s’,一个美元,美元b, c,美元美元d, e,美元$ f)需要9.1771278381348秒

其他回答

我用它来发送给用户的消息或其他“漂亮”类型的功能。例如,如果我知道我将使用用户的名字。

$name = 'Some dynamic name';

并在这种情况下使用多个消息。(例如,屏蔽或关注其他用户)

$messageBlock = 'You have blocked %s from accessing you.';
$messageFollow = 'Following %s is a great idea!';

你可以创建一个通用函数,对用户做一些事情,并添加这个字符串,无论句子的结构是什么,它都应该看起来很漂亮。我总是不喜欢仅仅把字符串附加在一起,不断地使用点符号,关闭和重新打开字符串,只是为了让一个句子看起来更好。我一开始是一个粉丝,像大多数人一样,但这似乎非常有用,当多个字符串需要被操纵,你不想硬编码变量的位置在每次。

想想看,哪个更好看?

return $messageOne === true ? $name.'. Please use the next example' : 'Hi '.$name.', how are you?'

Or

$message = $messageOne === true ? 'Option one %s' 
: ($messageTwo === true ? 'Option Two %s maybe?' : '%s you can choose from tons of grammatical instances and not have to edit variable placement and strings');

return sprintf($message, $name);

当然,这是一个额外的步骤,但如果你的条件检查做了一堆其他功能的事情,然后引号和追加开始在编码的功能。

一个是“输出”,另一个是“返回”,这是主要的区别之一。

printf()输出

sprintf()返回

Sprintf在格式化使用数字的字符串时特别有用。例如,

$oranges = -2.34;
echo sprintf("There are %d oranges in the basket", $oranges);

Output: There are -2 oranges in the basket

Oranges被格式化为整数(-2),但如果使用%u表示无符号值,则会换行为正数。为了避免这种行为,我使用绝对函数abs(),将数字四舍五入,如下所示:

$oranges = -5.67;
echo sprintf("There are %d oranges in the basket", abs($oranges));

Output: There are 5 oranges in the basket

最终的结果是一个具有高可读性、逻辑结构、清晰格式和根据需要灵活添加额外变量的语句。随着变量数量的增加以及操作这些变量的函数的组合,好处变得更加明显。最后一个例子:

$oranges = -3.14;
$apples = 1.5;
echo sprintf("There are %d oranges and %d apples", abs($oranges), abs($apples));

Output: There are 3 oranges and 4 apples

sprintf语句的左边清楚地表达了字符串和期望值的类型,而右边清楚地表达了使用的变量以及如何操作它们。

在一些典型的情况下,您需要更精确地控制输出格式。例如,根据特定值的长度,确保特定值前面填充了特定数量的空格,或者以特定的精确格式输出数字,这可能很棘手。

在PHP手册中有很多例子

还有你的“更容易写”的例子。虽然echo可能更容易编写,但sprintf更容易读取,特别是在有很多变量的情况下。

使用sprintf或printf的另一个原因可能是您希望让用户定义某些值的输出格式—您可以安全地允许他们定义与sprintf兼容的输出格式。

哦,你的例子有一部分是错的。Sprintf返回字符串,但echo不返回——echo立即输出它并且不返回任何东西,而Sprintf只是返回它。

Sprintf具有原始printf的所有格式化功能,这意味着除了在字符串中插入变量值外,您还可以做更多的事情。

例如,指定数字格式(十六进制,十进制,八进制),小数的数量,填充等等。谷歌用于printf,你会找到很多例子。你应该从维基百科上关于printf的文章开始。