我试图了解更多关于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;

我是不是遗漏了什么?


当前回答

你为什么要用它?

在为语言字符串使用(外部)源时,它被证明非常有用。如果你在一个给定的多语言字符串中需要固定数量的变量,你只需要知道正确的顺序:

en.txt

not_found    = "%s could not be found."
bad_argument = "Bad arguments for function %s."
bad_arg_no   = "Bad argument %d for function %s."

hu.txt

not_found    = "A keresett eljárás (%s) nem található."
bad_argument = "Érvénytelen paraméterek a(z) %s eljárás hívásakor."
bad_arg_no   = "Érvénytelen %d. paraméter a(z) %s eljárás hívásakor."

在多种语言中,插入的变量甚至不必在开头或结尾,只是它们的顺序很重要。

当然,你可以编写自己的函数来执行这个替换,毫无疑问,甚至会有一些小的性能提升,但它要快得多(假设你有一个类Language来读取语言字符串):

/**
 * throws exception with message($name = "ExampleMethod"):
 *  - using en.txt: ExampleMethod could not be found.
 *  - using hu.txt: A keresett eljárás (ExampleMethod) nem található.
 */
throw new Exception(sprintf(Language::Get('not_found'), $name));

/**
 * throws exception with message ($param_index = 3, $name = "ExampleMethod"):
 *  - using en.txt: Bad argument 3 for function ExampleMethod.
 *  - using hu.txt: Érvénytelen 3. paraméter a(z) ExampleMethod eljárás hívásakor.
 */
throw new Exception(sprintf(Language::Get('bad_arg_no'), $param_index, $name));

它还带有printf的全部功能,因此也是格式化多种类型变量的一行程序,例如:

浮点数输出精度,或 用前导零填充整数。

其他回答

sprintf有许多用例,但我使用它们的一种方式是在数据库中存储这样的字符串:'Hello, My Name is %s',或将其作为PHP类中的常量。这样当我想要使用该字符串时,我可以简单地这样做:

$name = 'Josh';
// $stringFromDB = 'Hello, My Name is %s';
$greeting = sprintf($stringFromDB, $name);
// $greetting = 'Hello, My Name is Josh'

本质上,它允许在代码中进行一些分离。如果我在代码中的许多地方使用“Hello, My Name is %s”,我可以在一个地方将其更改为“%s是我的名字”,并且它会自动更新其他任何地方,而不必去到每个实例并在连接中移动。

这样的:

"<p>Some big paragraph ".$a["name"]." again have to take care of space and stuff .". $a["age"]. "also would be hard to keep track of punctuations and stuff in a really ".$a["token"]. paragraph.";

也可以写成:

"<p>Some big paragraph {$a['name']} again have to take care of space and stuff .{$a['age']} also would be hard to keep track of punctuations and stuff in a really {$a['token']} paragraph.";

在我看来,这更容易理解,但我可以看到本地化或格式化的用途。

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

$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);

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

我发现的最好的理由是,它允许你把所有的语言字符串放在你的语言文件中,人们可以根据需要翻译和排序它们-但你仍然知道,无论字符串的格式是什么-你希望显示用户的名字。

例如,您的网站会在页面顶部显示“欢迎返回[[User]]”。作为程序员,你不知道或不关心UI人员将如何编写它——你只知道用户名将显示在消息中的某个地方。

因此,您可以将消息嵌入到代码中,而不必担心该消息实际上是什么。

只需文件 (EN_US):

...
$lang['welcome_message'] = 'Welcome back %s';
...

然后,通过在实际的php代码中使用它,就可以支持任何语言的任何类型的消息。

sprintf($lang['welcome_message'], $user->name())

如果您使用过C/ c++,那么您应该已经习惯了sprintf函数。 很有可能第二条线的效率较低。Echo被设计为输出命令,而sprintf被设计为执行字符串令牌替换。我不是PHP专家,但我怀疑回声涉及到更多的对象。如果它像Java那样工作,那么每当有东西添加到列表中时,它都会创建一个新的字符串,因此最终会创建4个字符串。