我试图了解更多关于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的全部功能,因此也是格式化多种类型变量的一行程序,例如:

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

其他回答

define('TEXT_MESSAGE', 'The variable "%s" is in the middle!');

sprintf(TEXT_MESSAGE, "Var1");
sprintf(TEXT_MESSAGE, "Var2");
sprintf(TEXT_MESSAGE, "Var3");

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

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

如前所述,它允许格式化输入数据。例如,强制2dp, 4位数字等。它对于构建MySQL查询字符串非常有用。

另一个优点是,它允许将字符串的布局与输入的数据分开,就像输入参数一样。例如,在MySQL查询的情况下:

// For security, you MUST sanitise ALL user input first, eg:
$username = mysql_real_escape_string($_POST['username']); // etc.
// Now creating the query:
$query = sprintf("INSERT INTO `Users` SET `user`='%s',`password`='%s',`realname`='%s';", $username, $passwd_hash, $realname);

当然,这种方法还有其他用途,比如将输出打印为HTML等。

编辑:出于安全原因,当使用上述技术时,必须在使用此方法之前使用mysql_real_escape_string()清除所有输入变量,以防止MySQL插入攻击。如果你解析未经处理的输入,你的网站和服务器就会被黑客攻击。(当然,那些完全由你的代码构造并保证安全的变量除外。)

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

在PHP手册中有很多例子

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

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

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

这样的:

"<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.";

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