Sprintf ()가 아무것도 출력하지 않는 이유는 무엇입니까?

StackOverflow https://stackoverflow.com/questions/1021743

  •  06-07-2019
  •  | 
  •  

문제

나는 전에이 기능을 사용한 적이 없지만 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());

다른 팁

sprintf() 결과를 문자열에 인쇄합니다. printf() 표준 출력으로 인쇄합니다.

printf(currentDateTime());

다음과 같습니다.

echo sprintf(currentDateTime());

sprintf ()는 문자열을 반환하는 동안 printf ()가 문자열을 출력합니다. 따라서 다음과 같은 작업을 수행해야합니다.

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;
}

$output = sprintf(currentDateTime());
printf($output);

http://www.php.net/sprintf

http://www.php.net/printf

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top