2009-06-20 2 views
17

나는 sprintf()에 대해 많이 읽은 후에이 함수를 사용한 적이 없다. 나는 그것을 알아야한다고 결정했다.왜 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() 함수를 제대로 사용합니까?

답변

57

sprintf() 문자열, printf() 표시를 반환

printf(currentDateTime()); 

은 동일합니다.

다음 두

은 동일하다 :의 printf()는 문자열을 출력하면서

printf(currentDateTime()); 
print sprintf(currentDateTime()); 
+24

내가 '()'** 침묵 **'의 printf를': – deed02392

+0

@ deed02392 생각하여 기억 : 그래서 당신은 다음과 같은 뭔가를해야 할 것 ,'s'는 "문자열"을 나타내는 것이 아닌가? – Pacerier

14

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

echo sprintf(currentDateTime()); 
6

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

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

http://www.php.net/sprintf

http://www.php.net/printf

관련 문제