{"id":1818,"date":"2021-07-18T16:03:33","date_gmt":"2021-07-18T13:03:33","guid":{"rendered":"https:\/\/www.etemkeskin.com\/?p=1818"},"modified":"2021-07-18T16:06:21","modified_gmt":"2021-07-18T13:06:21","slug":"printf-print-echo-sprintf-vprintf-vfprintf-vsprintf-statements-in-php","status":"publish","type":"post","link":"https:\/\/www.etemkeskin.com\/index.php\/2021\/07\/18\/printf-print-echo-sprintf-vprintf-vfprintf-vsprintf-statements-in-php\/","title":{"rendered":"Printf, print, echo, sprintf, vprintf, vfprintf, vsprintf Statements in PHP"},"content":{"rendered":"\n<p>There are many ways to output string expressions in <strong>PHP<\/strong>. In this post, I will explain the expressions <code>echo, print, printf, sprintf, vprintf, vfprintf, vsprintf<\/code>, which seem to be similar to each other but have various differences, and how they are used.<\/p>\n\n\n\n<h3>1.) echo :   <\/h3>\n\n\n\n<ul><li>It prints a string output to the screen. String can contain <strong>HTML<\/strong> expressions.<\/li><li>It is not a <strong>real function<\/strong>. It comes with the structure of the language.<\/li><li>It does not return a value when executed. Doesn&#8217;t behave like a function<\/li><li>The echo statement can be used as <strong>echo<\/strong> and <strong>echo()<\/strong>.<\/li><li>If <strong>double quotes<\/strong> are used, variables can be used inside the string expression.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;showPanel&quot;:false,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">$x = 5;\necho &quot;Hello world!&quot;;\necho &quot;&lt;h2&gt; PHP is wonderful language. &lt;\/h2&gt;&quot;;\necho &quot;Number is : $x&quot;;\n\/\/ Output: Hello world!\n\/\/ Output: PHP is wonderful language.\n\/\/ Output: Number is : 5 <\/pre><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"445\" height=\"163\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/07\/image-1.png\" alt=\"\" class=\"wp-image-1838\"\/><figcaption>Usage of the echo statement.<\/figcaption><\/figure><\/div>\n\n\n\n<h3>2.) print:<\/h3>\n\n\n\n<ul><li>Outputs a formatted string.<\/li><li>Since the <strong>print<\/strong> statement is not a real function, there is no need to use parentheses. It comes with the structure of the language.<\/li><li><strong>It always returns 1 when executed.<\/strong><\/li><\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;showPanel&quot;:false,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">$x = print &quot;Hello world! &lt;br&gt;&quot;;\necho $x;\n\/\/Output :  Hello world!\n\/\/Output : 1<\/pre><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"153\" height=\"71\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/07\/image-2.png\" alt=\"\" class=\"wp-image-1842\"\/><figcaption>Screenshot of print statement<\/figcaption><\/figure><\/div>\n\n\n\n<h3>3.) printf:<\/h3>\n\n\n\n<ul><li>Outputs a formatted string.<\/li><li>Returns the <strong>length of the string output<\/strong> when executed.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;showPanel&quot;:false,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">$number = 5;\n$str = &quot;London&quot;;\n$x = printf(&quot;There are %u million people in %s. &lt;br&gt;&quot;,$number,$str);\necho $x;\n\/\/Output: There are 5 million people in London.\n\/\/Output: 42<\/pre><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"392\" height=\"69\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/07\/image-3.png\" alt=\"\" class=\"wp-image-1844\"\/><figcaption>Screenshot of printf statement<\/figcaption><\/figure><\/div>\n\n\n\n<h3><strong>4.) sprintf:<\/strong><\/h3>\n\n\n\n<ul><li>Returns the formatted string.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;showPanel&quot;:false,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">$number = 5;\n$str = &quot;London&quot;;\n$x = sprintf(&quot;There are %u million people in %s. &lt;br&gt;&quot;,$number,$str);\necho $x;\n\/\/Output: There are 5 million people in London.<\/pre><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"373\" height=\"37\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/07\/image-4.png\" alt=\"\" class=\"wp-image-1848\"\/><figcaption>Screen output of the value returned in the sprintf expression<\/figcaption><\/figure><\/div>\n\n\n\n<p>If the arguments are to be used in more than one place, the <strong>\\$<\/strong> expression is used.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;showPanel&quot;:false,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">$number = 5;\n$str = &quot;London&quot;;\n$x = sprintf(&quot;There are %1\\$u million people in %s. &lt;br&gt; Second usage place: %1\\$u&quot;,$number,$str);\necho $x;\n\/\/Output: There are 5 million people in 5.\n\t     Second usage place: 5<\/pre><\/div>\n\n\n\n<h3><strong>5.) vprintf:<\/strong><\/h3>\n\n\n\n<ul><li>Output a formatted string.<\/li><li>It behaves like<strong> printf<\/strong>, except it takes <strong>array <\/strong>as an argument instead of variables.<\/li><li>Returns the length of the formatted string.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;showPanel&quot;:false,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">$number = 5;\n$str = &quot;London&quot;;\n$x = vprintf(&quot;There are %u million people in %s. &lt;br&gt;&quot;, [$number,$str]);\necho $x;\n\/\/Output: There are 5 million people in London.\n\/\/Output: 42<\/pre><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"383\" height=\"66\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/07\/image-5.png\" alt=\"\" class=\"wp-image-1855\"\/><figcaption>Screenshot of vprintf statement<\/figcaption><\/figure><\/div>\n\n\n\n<h3>6.) <strong><em>vsprintf:<\/em><\/strong><\/h3>\n\n\n\n<ul><li>Returns the formatted string value.<\/li><li>It behaves like <strong>sprintf<\/strong>, except it takes <strong>array<\/strong> as an argument instead of variables.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;showPanel&quot;:false,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">$number = 5;\n$str = &quot;London&quot;;\n$x = vsprintf(&quot;There are %u million people in %s. &lt;br&gt;&quot;, [$number,$str]);\necho $x;\n\/\/Output: There are 5 million people in London.<\/pre><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"387\" height=\"44\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/07\/image-6.png\" alt=\"\" class=\"wp-image-1858\"\/><figcaption>Screen output of the value returned in the vsprintf expression<\/figcaption><\/figure><\/div>\n\n\n\n<h3>7.) vfprintf:<\/h3>\n\n\n\n<ul><li>Writes a formatted string to a file.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;showPanel&quot;:false,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">$number = 5;\n$str = &quot;London&quot;;\n$file = fopen(&quot;test.txt&quot;,&quot;w&quot;);\nvfprintf(&quot;There are %u million people in %s. &lt;br&gt;&quot;, [$number,$str]);<\/pre><\/div>\n\n\n\n<p>test.txt  dosyas\u0131na <em>There are 5 million people in London expression<\/em> is written in <strong>test.txt<\/strong> .<\/p>\n\n\n\n<p>The table below shows the format values you can use for variables in string expressions.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><em>format<\/em><\/td><td><strong>POSSIBLE FORMAT VALUES<\/strong><br>%% &#8211; Returns a percent sign<br>%b &#8211; Binary number<br>%c &#8211; The character according to the ASCII value<br>%d &#8211; Signed decimal number (negative, zero or positive)<br>%e &#8211; Scientific notation using a lowercase (e.g. 1.2e+2)<br>%E &#8211; Scientific notation using a uppercase (e.g. 1.2E+2)<br>%u &#8211; Unsigned decimal number (equal to or greather than zero)<br>%f &#8211; Floating-point number (local settings aware)<br>%F &#8211; Floating-point number (not local settings aware)<br>%g &#8211; shorter of %e and %f<br>%G &#8211; shorter of %E and %f<br>%o &#8211; Octal number%s &#8211; String<br>%x &#8211; Hexadecimal number (lowercase letters)<br>%X &#8211; Hexadecimal number (uppercase letters)<\/td><\/tr><\/tbody><\/table><figcaption>Values to use when formatting in PHP.<\/figcaption><\/figure>\n\n\n\n<p>Good luck&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are many ways to output string expressions in PHP. In this post, I will explain the expressions echo, print, printf, sprintf, vprintf, vfprintf, vsprintf, which seem to be similar to each other but have various differences, and how they are used. 1.) echo : It prints a string output to the screen. String can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Printf, print, echo, sprintf, vprintf, vfprintf, vsprintf Statements in PHP - blog website<\/title>\n<meta name=\"description\" content=\"There are many ways to output string expressions in PHP. In this post, I will explain the expressions echo, print, printf, sprintf, vprintf, vfprintf, vsprintf, which seem to be similar to each other but have various differences, and how they are used.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.etemkeskin.com\/index.php\/2021\/07\/18\/printf-print-echo-sprintf-vprintf-vfprintf-vsprintf-statements-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"tr_TR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Printf, print, echo, sprintf, vprintf, vfprintf, vsprintf Statements in PHP - blog website\" \/>\n<meta property=\"og:description\" content=\"There are many ways to output string expressions in PHP. In this post, I will explain the expressions echo, print, printf, sprintf, vprintf, vfprintf, vsprintf, which seem to be similar to each other but have various differences, and how they are used.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.etemkeskin.com\/index.php\/2021\/07\/18\/printf-print-echo-sprintf-vprintf-vfprintf-vsprintf-statements-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"blog website\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-18T13:03:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-18T13:06:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/07\/image-1.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Tahmini okuma s\u00fcresi\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 dakika\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.etemkeskin.com\/#website\",\"url\":\"https:\/\/www.etemkeskin.com\/\",\"name\":\"blog website\",\"description\":\"Etem KESK\\u0130N\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/www.etemkeskin.com\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"tr\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/07\/18\/printf-print-echo-sprintf-vprintf-vfprintf-vsprintf-statements-in-php\/#primaryimage\",\"inLanguage\":\"tr\",\"url\":\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/07\/image-1.png\",\"contentUrl\":\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/07\/image-1.png\",\"width\":445,\"height\":163},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/07\/18\/printf-print-echo-sprintf-vprintf-vfprintf-vsprintf-statements-in-php\/#webpage\",\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/07\/18\/printf-print-echo-sprintf-vprintf-vfprintf-vsprintf-statements-in-php\/\",\"name\":\"Printf, print, echo, sprintf, vprintf, vfprintf, vsprintf Statements in PHP - blog website\",\"isPartOf\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/07\/18\/printf-print-echo-sprintf-vprintf-vfprintf-vsprintf-statements-in-php\/#primaryimage\"},\"datePublished\":\"2021-07-18T13:03:33+00:00\",\"dateModified\":\"2021-07-18T13:06:21+00:00\",\"author\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\"},\"description\":\"There are many ways to output string expressions in PHP. In this post, I will explain the expressions echo, print, printf, sprintf, vprintf, vfprintf, vsprintf, which seem to be similar to each other but have various differences, and how they are used.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/07\/18\/printf-print-echo-sprintf-vprintf-vfprintf-vsprintf-statements-in-php\/#breadcrumb\"},\"inLanguage\":\"tr\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.etemkeskin.com\/index.php\/2021\/07\/18\/printf-print-echo-sprintf-vprintf-vfprintf-vsprintf-statements-in-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/07\/18\/printf-print-echo-sprintf-vprintf-vfprintf-vsprintf-statements-in-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Ana sayfa\",\"item\":\"https:\/\/www.etemkeskin.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Printf, print, echo, sprintf, vprintf, vfprintf, vsprintf Statements in PHP\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\",\"name\":\"etemkeskin\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.etemkeskin.com\/#personlogo\",\"inLanguage\":\"tr\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6af0148b790691ed24ae245fb3dc773b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6af0148b790691ed24ae245fb3dc773b?s=96&d=mm&r=g\",\"caption\":\"etemkeskin\"},\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/author\/etemkeskinyahoo-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/1818"}],"collection":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/comments?post=1818"}],"version-history":[{"count":63,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/1818\/revisions"}],"predecessor-version":[{"id":1911,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/1818\/revisions\/1911"}],"wp:attachment":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/media?parent=1818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/categories?post=1818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/tags?post=1818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}