Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't sprintf() output anything?

Tags:

php

printf

I've never used these functions before but after reading a lot about sprintf(), I decided I should get to know it.

So I went ahead and did the following.

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

It prints nothing. Using the printf() function on the other hand:

printf(currentDateTime());

It prints the result just fine. So what's the difference between these 2 functions and how do I properly use the sprintf() function?

like image 273
KdgDev Avatar asked Jun 20 '09 14:06

KdgDev


1 Answers

sprintf() returns a string, printf() displays it.

The following two are equal:

printf(currentDateTime());
print sprintf(currentDateTime());
like image 85
molf Avatar answered Oct 10 '22 14:10

molf