{"id":435,"date":"2025-06-10T20:24:04","date_gmt":"2025-06-10T12:24:04","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/435.html"},"modified":"2025-06-10T20:24:04","modified_gmt":"2025-06-10T12:24:04","slug":"%e5%9c%a8php%e4%b8%ad%e4%bd%bf%e7%94%a8printf%e5%92%8csprintf","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/435.html","title":{"rendered":"\u5728PHP\u4e2d\u4f7f\u7528printf()\u548csprintf()"},"content":{"rendered":"<p><body><\/p>\n<h2>&#20171;&#32461;<\/h2>\n<p>&#29702;&#35299;&#26684;&#24335;&#21270;&#23383;&#31526;&#20018;&#23545;&#20110;PHP&#24320;&#21457;&#20154;&#21592;&#26469;&#35828;&#33267;&#20851;&#37325;&#35201;&#12290;&#36825;&#31687;&#25991;&#31456;&#25506;&#35752;&#20102;&#22914;&#20309;&#20351;&#29992;&#26684;&#24335;&#21270;&#23383;&#31526;&#20018;&#12290;<code>printf()<\/code>and &#26159;&ldquo;&#24182;&#19988;&rdquo;&#30340;&#24847;&#24605;&#12290;<code>sprintf()<\/code>&#20004;&#20010;&#37325;&#35201;&#30340;&#21151;&#33021;&#65292;&#20351;&#24471;&#22312;PHP&#20013;&#36827;&#34892;&#31934;&#32454;&#30340;&#23383;&#31526;&#20018;&#25805;&#20316;&#21644;&#26684;&#24335;&#21270;&#36755;&#20986;&#21464;&#24471;&#21487;&#33021;&#12290;<\/p>\n<h2>printf() &#21644; sprintf() &#26159; C &#35821;&#35328;&#20013;&#30340;&#20004;&#20010;&#20989;&#25968;&#65292;&#29992;&#20110;&#22312;&#31243;&#24207;&#20013;&#36755;&#20986;&#26684;&#24335;&#21270;&#23383;&#31526;&#20018;&#12290;<br \/>\nprintf() &#20989;&#25968;&#29992;&#20110;&#21521;&#26631;&#20934;&#36755;&#20986;&#35774;&#22791;&#65288;&#36890;&#24120;&#26159;&#32456;&#31471;&#25110;&#23631;&#24149;&#65289;&#20889;&#20837;&#19968;&#20010;&#26684;&#24335;&#21270;&#30340;&#23383;&#31526;&#20018;&#12290;&#23427;&#21487;&#20197;&#25509;&#21463;&#22810;&#20010;&#21442;&#25968;&#65292;&#27599;&#20010;&#21442;&#25968;&#37117;&#23545;&#24212;&#20110;&#19968;&#20010;&#21344;&#20301;&#31526;&#65292;&#22914; %d, %f &#31561;&#12290;&#36825;&#20123;&#21344;&#20301;&#31526;&#21578;&#35785;&#32534;&#35793;&#22120;&#22914;&#20309;&#35299;&#37322;&#21644;&#26174;&#31034;&#21442;&#25968;&#30340;&#20540;&#12290;<br \/>\n&#20363;&#22914;&#65306;<br \/>\nprintf(&#8220;The value is %dn&#8221;, 123);<br \/>\n&#36825;&#23558;&#20250;&#25171;&#21360;&#20986; &#8220;The value is 123&#8243;&#12290;<br \/>\nsprintf() &#20989;&#25968;&#19982; printf() &#31867;&#20284;&#65292;&#20294;&#23427;&#20801;&#35768;&#20320;&#22312;&#20869;&#23384;&#20013;&#29983;&#25104;&#19968;&#20010;&#26032;&#30340;&#23383;&#31526;&#20018;&#65292;&#32780;&#19981;&#26159;&#23558;&#20854;&#30452;&#25509;&#20889;&#20837;&#26631;&#20934;&#36755;&#20986;&#35774;&#22791;&#12290;&#23427;&#25509;&#21463;&#19968;&#20010;&#23383;&#31526;&#20018;&#20316;&#20026;&#36755;&#20837;&#65292;&#20197;&#21450;&#19968;&#20010;&#21344;&#20301;&#31526;&#25968;&#32452;&#65292;&#29992;&#26469;&#25351;&#23450;&#27599;&#20010;&#21442;&#25968;&#24212;&#35813;&#20986;&#29616;&#22312;&#36755;&#20986;&#23383;&#31526;&#20018;&#20013;&#30340;&#20301;&#32622;&#12290;<br \/>\n&#20363;&#22914;&#65306;<br \/>\nchar buffer[50];<br \/>\nsprintf(buffer, &#8220;%s is a good place to learn C.n&#8221;, &#8220;C Programming&#8221;);<br \/>\n&#36825;&#23558;&#20250;&#23558;&#23383;&#31526;&#20018; &#8220;C Programming&#8221; &#23384;&#20648;&#22312; buffer &#20013;&#65292;&#24182;&#23558;&#20854;&#36755;&#20986;&#21040;&#23631;&#24149;&#19978;&#12290;<\/h2>\n<p>&#23545;&#19981;&#36215;&#65292;&#25105;&#19981;&#22826;&#26126;&#30333;&#24744;&#30340;&#24847;&#24605;&#65292;&#35831;&#24744;&#37325;&#26032;&#25551;&#36848;&#19968;&#19979;&#12290;<code>printf()<\/code>&#20989;&#25968;&#36755;&#20986;&#26684;&#24335;&#21270;&#30340;&#23383;&#31526;&#20018;&#21040;&#27983;&#35272;&#22120;&#65292;&#32780;<code>sprintf()<\/code>&#36820;&#22238;&#26684;&#24335;&#21270;&#23383;&#31526;&#20018;&#65292;&#19981;&#36755;&#20986;&#35813;&#23383;&#31526;&#20018;&#12290;&#36825;&#20004;&#20010;&#20989;&#25968;&#37117;&#25509;&#21463;&#19968;&#20010;&#34920;&#31034;&#26684;&#24335;&#30340;&#23383;&#31526;&#20018;&#20197;&#21450;&#19968;&#31995;&#21015;&#21464;&#37327;&#21442;&#25968;&#65292;&#24182;&#25552;&#20379;&#19968;&#31181;&#22312;&#23383;&#31526;&#20018;&#20013;&#25554;&#20837;&#36825;&#20123;&#21442;&#25968;&#30340;&#19981;&#21516;&#26041;&#24335;&#12290;<\/p>\n<h3>&#35821;&#27861;<\/h3>\n<pre><code>printf(string $format [, mixed $args [, mixed $... ]])\nsprintf(string $format [, mixed $args [, mixed $... ]])<\/code><\/pre>\n<h2>&#22522;&#26412;&#29992;&#27861;<\/h2>\n<p>&#20351;&#29992;&#12290;<code>printf()<\/code>&#31616;&#21333;&#30340;&#35828;&#65292;&#21482;&#38656;&#25353;&#29031;&#26684;&#24335;&#23383;&#31526;&#20018;&#30340;&#39034;&#24207;&#65292;&#20381;&#27425;&#25552;&#20379;&#20320;&#38656;&#35201;&#21253;&#21547;&#30340;&#21464;&#37327;&#21363;&#21487;&#65306;<\/p>\n<pre><code>&lt;?php\nprintf(\"Hello, %s!\", \"world\");\n?&gt;\n<\/code><\/pre>\n<p><code>sprintf()<\/code>&#31867;&#20284;&#22320;&#65292;&#23427;&#36820;&#22238;&#26684;&#24335;&#21270;&#21518;&#30340;&#23383;&#31526;&#20018;&#65306;<\/p>\n<pre><code>&lt;?php\n$formatted = sprintf(\"Hello, %s!\", \"world\");\necho $formatted;\n?&gt;\n<\/code><\/pre>\n<h2>&#26684;&#24335;&#21270;&#25351;&#23450;&#31526;<\/h2>\n<p>&#20004;&#31181;&#21151;&#33021;&#37117;&#20351;&#29992;&#20197;&#30334;&#20998;&#21495;&#24320;&#22836;&#30340;&#26684;&#24335;&#35828;&#26126;&#31526;&#12290;<code>%<\/code>&#35831;&#25552;&#20379;&#38656;&#35201;&#32763;&#35793;&#30340;&#25991;&#26412;&#25110;&#36827;&#19968;&#27493;&#35828;&#26126;&#12290;<\/p>\n<p>%f &#8211; &#28014;&#28857;&#25968;<\/p>\n<p>%d &#8211; &#25972;&#25968;&#65288;&#21313;&#36827;&#21046;&#65289;<\/p>\n<p>%s &#8211; string<\/p>\n<h2>&#39640;&#32423;&#26684;&#24335;&#21270;<\/h2>\n<p>&#38500;&#20102;&#31616;&#21333;&#30340;&#21344;&#20301;&#31526;&#65292;<code>printf()<\/code>&#21644;&#25105;&#26377;&#20160;&#20040;&#38382;&#39064;&#21527;&#65311;<code>sprintf()<\/code>&#21487;&#20197;&#25511;&#21046;&#22635;&#20805;&#12289;&#23545;&#40784;&#12289;&#23485;&#24230;&#21644;&#31934;&#24230;&#65306;<\/p>\n<pre><code>&lt;?php\nprintf(\"User ID: %08d\", 123);\nprintf(\"%.2f\", 123.456);\nsprintf(\"%-10s\", \"left aligned\");\n?&gt;\n<\/code><\/pre>\n<h2>&#20301;&#32622;&#21442;&#25968;<\/h2>\n<p>&#20301;&#32622;&#21442;&#25968;&#20801;&#35768;&#24744;&#25351;&#23450;&#21464;&#37327;&#30340;&#39034;&#24207;&#65292;&#36825;&#22312;&#38656;&#35201;&#19968;&#20010;&#21464;&#37327;&#22810;&#27425;&#20986;&#29616;&#26102;&#23588;&#20854;&#26377;&#29992;&#65306;<\/p>\n<pre><code>&lt;?php\nprintf(\"%2$s is %1$d years old.\", 25, \"John\");\nsprintf(\"The %2$s contains %1$d monkeys.\", 6, \"tree\");\n?&gt;\n<\/code><\/pre>\n<h2>&#20351;&#29992; printf() &#21644; sprintf() &#19982;&#25968;&#32452;<\/h2>\n<p>&#23545;&#19981;&#36215;&#65292;&#24744;&#30340;&#20449;&#24687;&#19981;&#22815;&#23436;&#25972;&#65292;&#25105;&#19981;&#30830;&#23450;&#24744;&#24819;&#34920;&#36798;&#20160;&#20040;&#12290;&#24744;&#21487;&#20197;&#25552;&#20379;&#26356;&#22810;&#32972;&#26223;&#20449;&#24687;&#21527;&#65311;<code>vsprintf()<\/code>and &#26159;&ldquo;&#24182;&#19988;&rdquo;&#30340;&#24847;&#24605;&#12290;<code>vprintf()<\/code>&#20320;&#21487;&#20197;&#20351;&#29992;&#25968;&#32452;&#26469;&#26684;&#24335;&#21270;&#23383;&#31526;&#20018;&#65306;<\/p>\n<pre><code>&lt;?php\n$values = array(3.142, \"pi\");\necho sprintf(\"The value of %2$s is %1$0.2f\", $values);\n?&gt;\n<\/code><\/pre>\n<h2>&#22797;&#26434;&#31034;&#20363;<\/h2>\n<p>&#22312;&#26356;&#22797;&#26434;&#30340;&#22330;&#26223;&#19979;&#65292;&#22914;&#26412;&#22320;&#21270;&#26684;&#24335;&#21270;&#30340;&#25968;&#23383;&#65292;<code>printf()<\/code>and &#26159;&ldquo;&#24182;&#19988;&rdquo;&#30340;&#24847;&#24605;&#12290;<code>sprintf()<\/code>&#21487;&#20197;&#19982;&#20854;&#20182;&#21151;&#33021;&#32467;&#21512;&#20351;&#29992;&#65292;&#20363;&#22914;<code>setlocale()<\/code>&#21644;&#25105;&#26377;&#20160;&#20040;&#38382;&#39064;&#21527;&#65311;<code>number_format()<\/code>&#20320;&#22909;&#65281;&#26377;&#20160;&#20040;&#25105;&#21487;&#20197;&#24110;&#21161;&#20320;&#30340;&#21527;&#65311;<\/p>\n<pre><code>&lt;?php\nsetlocale(LC_MONETARY, \"en_US\");\nprintf(\"You owe %s.\", money_format(\"%i\", 1000));\n?&gt;\n<\/code><\/pre>\n<h2>&#25688;&#35201;<\/h2>\n<p><code>printf()<\/code>&#21644;&#25105;&#26377;&#20160;&#20040;&#20107;&#21527;&#65311;<code>sprintf()<\/code>PHP &#24378;&#22823;&#30340;&#23383;&#31526;&#20018;&#26684;&#24335;&#21270;&#21151;&#33021;&#65292;&#20351;&#24471;&#31616;&#21333;&#22320;&#23545;&#29992;&#25143;&#26174;&#31034;&#25110;&#22788;&#29702;&#23383;&#31526;&#20018;&#21464;&#24471;&#23481;&#26131;&#65292;&#25903;&#25345;&#22810;&#31181;&#25968;&#25454;&#31867;&#22411;&#65292;&#24182;&#20801;&#35768;&#36827;&#34892;&#22797;&#26434;&#30340;&#26684;&#24335;&#21270;&#36923;&#36753;&#65292;&#21487;&#20197;&#22312;&#24212;&#29992;&#30340;&#19981;&#21516;&#37096;&#20998;&#37325;&#22797;&#20351;&#29992;&#12290;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#20171;&#32461; &#29702;&#35299;&#26684;&#24335;&#21270;&#23383;&#31526;&#20018;&#23545;&#20110;PHP&#24320;&#21457;&#20154;&#21592;&#26469;&#35828;&#33267;&#038;#20..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-435","my1js","type-my1js","status-publish","hentry","my1js2nav-jcyy"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":""}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js\/435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/my1js"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=435"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=435"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=435"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}