php - 为什么 sprintf() 不输出任何东西?

标签 php printf

我以前从未使用过这些函数,但在阅读了很多关于 sprintf() 的内容后,我决定我应该了解它。

所以我继续进行了以下操作。

function currentDateTime() {
  list($micro, $Unixtime) = explode(" ",microtime());
  $sec= $micro + date("s", $Unixtime);
  $sec = mb_ereg_replace(sprintf('%d', $sec), "", ($micro + date("s", $Unixtime)));
  return date("Y-m-d H:i:s", $Unixtime).$sec;
}

sprintf(currentDateTime());

它什么都不打印。另一方面使用 printf() 函数:

printf(currentDateTime());

它打印的结果很好。那么这两个函数有什么区别,我该如何正确使用 sprintf() 函数?

最佳答案

sprintf() 返回一个字符串,printf() 显示它。

下面两个是相等的:

printf(currentDateTime());
print sprintf(currentDateTime());

关于php - 为什么 sprintf() 不输出任何东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1021743/

相关文章:

php - 使回显结果可点击并运行新查询

php - drupal:表单API,根据输入动态隐藏或显示字段

c - 如何在参数数量可变的函数中传递参数列表?

c - 使用 set_usart_base((void *)) EVK1105 dev 时的隐式函数声明。木板。爱特梅尔

php - laravel 从数据库检索数据需要太多时间

php - 无法读取数据,因为它的格式不正确

c - pow(1,0) 返回 0?

C - 在使用 sprintf 和打印字符串时遇到问题

java - printf 与参数后给出的字符串串联不起作用

php - 属于许多获取所有行(Laravel)