使用带参数的printf

时间:2011-11-22 03:20:55

标签: php parameters printf

如何使用 printf 处理参数而不管其类型如何? (类似于PDO中预准备语句中的绑定参数)。

E.g:

printf("Hello $s, you have $s new notifications.", $username, $newNotifications);

我尝试了类似的东西但没有用。

3 个答案:

答案 0 :(得分:1)

查看man page

基本上,当您使用$s时,您正在使用%s。但您可以使用printecho使用字符串:

print "Hello ".$username.", you have ".$newNotifications." new notifications.";

答案 1 :(得分:0)

printf需要知道如何格式化参数。您可以使用echo连接运算符.代替 echo "Hello " . $username . ", you have " . $newNotifications . " new notifications.";

{{1}}

答案 2 :(得分:0)

echo "Hello $username, you have $newNotifications new notifications.";

请注意,良好的语法高亮显示器将强调字符串中的变量,以获得更好的可读性,这使得这种方式更加出色。