PHP/sprintf()

解説 結果を文字列として返す

フォーマットされた文字列を返す

sprintf(フォーマットを定義する文字列, [差し込む変数1, [差し込む変数2], ...])

sample1 false
sample1code
$s1Num = 123;
if(sprintf($s1Num) === 123){
  echo "true";
} else {
  echo "false";
}
sample2 00002015
sample2code
$s2Str = "2018";
//8桁になるように0埋め
echo sprintf("%08s", $s2Str);
	
sample3 私は梨が好物だ。
sample3code
$format = "%sは%sが%sだ。";
$str01 = '私';
$str02  = '梨';
$str03 = '好物';
$sent = sprintf($format, $str01, $str02, $str03);
echo $sent;
	
sample4 リンゴは好物が僕とリンゴだ。
sample4code
$format = "%2\$sは%3\$sが%1\$sと%2\$sだ。";
$str01 = '僕';
$str02  = 'リンゴ';
$str03 = '好物';
$sent = sprintf($format, $str01, $str02, $str03);
echo $sent;