{"id":1441,"date":"2018-02-02T16:07:08","date_gmt":"2018-02-02T21:07:08","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=1441"},"modified":"2018-02-02T16:07:08","modified_gmt":"2018-02-02T21:07:08","slug":"php-output-text-variables-html-markup-using-print-echo","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/02\/02\/php-output-text-variables-html-markup-using-print-echo\/","title":{"rendered":"PHP: How to output text, variables, and HTML markup using print and echo"},"content":{"rendered":"<p>In PHP there are two ways to &#8220;print&#8221; HTML markup, text, and variables, using <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">print<\/code> and <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">echo<\/code>. These are essentially interchangeable, though <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">print<\/code> is slower, so best practice is to use <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">echo<\/code>. &#8220;print&#8221; has a return value of 1 (so it can be used in expressions), while &#8220;echo&#8221; does not.\u00a0 print can take one argument, while echo can take multiple arguments.<\/p>\n<p>This code demonstrates how both echo and print work interchangeably. It also shows how to print a mixture of HTML markup, text, and variable data in PHP.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php\r\n$txt1 = \"Using echo\";\r\n$txt2 = \"writephponline.com\";\r\n$x = 89;\r\n$y = 10;\r\n\r\necho \"&lt;h3&gt;\" . $txt1 . \"&lt;\/h3&gt;\";\r\necho $txt2 . \" is a good place to test PHP code.&lt;br&gt;\";\r\necho \"89 x 10 = \". $x * $y;\r\n\r\n$txt1 = \"Using print\";\r\n$txt2 = \"phptester.net\";\r\n$x = 45;\r\n$y = 8;\r\n\r\nprint \"&lt;h3&gt;\" . $txt1 . \"&lt;\/h3&gt;\";\r\nprint $txt2 . \" is a good place to test PHP code.&lt;br&gt;\";\r\nprint \"45 x 8 = \". $x * $y;\r\n?&gt;<\/pre>\n<p>Which yields:<\/p>\n<h3>Using echo<\/h3>\n<p>writephponline.com is a good place to test PHP code.<br \/>\n89 x 10 = 890<\/p>\n<h3>Using print<\/h3>\n<p>phptester.net is a good place to test PHP code.<br \/>\n45 x 8 = 360<\/p>\n<p>&nbsp;<\/p>\n<p>This code demonstrates that echo can output multiple comma separated arguments:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php\r\necho \"This \", \"string \", \"was \", \"made \", \"with multiple parameters.\";\r\n?&gt;<\/pre>\n<p>which yields:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">This string was made with multiple parameters.<\/pre>\n<p>However, print does not support multiple arguments. PHP will give an error when it sees a comma as an argument to print. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php\r\nprint \"This \", \"string \", \"was \", \"made \", \"with multiple parameters.\";\r\n?&gt;<\/pre>\n<p>yields:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">FATAL ERROR syntax error, unexpected ',' on line number 2<\/pre>\n<p>For more information about print and echo see:<br \/>\n<a href=\"https:\/\/www.w3schools.com\/php\/php_echo_print.asp\">https:\/\/www.w3schools.com\/php\/php_echo_print.asp<\/a><br \/>\n<a href=\"http:\/\/php.net\/manual\/en\/function.print.php\">http:\/\/php.net\/manual\/en\/function.print.php<\/a><br \/>\n<a href=\"http:\/\/php.net\/manual\/en\/function.echo.php\">http:\/\/php.net\/manual\/en\/function.echo.php<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In PHP there are two ways to &#8220;print&#8221; HTML markup, text, and variables, using print and echo. These are essentially interchangeable, though print is slower, so best practice is to use echo. &#8220;print&#8221; has a return value of 1 (so it can be used in expressions), while &#8220;echo&#8221; does not.\u00a0 print can take one argument, &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/02\/02\/php-output-text-variables-html-markup-using-print-echo\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">PHP: How to output text, variables, and HTML markup using print and echo<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[97],"tags":[98,69],"class_list":["post-1441","post","type-post","status-publish","format-standard","hentry","category-php-language","tag-echo","tag-php"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=1441"}],"version-history":[{"count":12,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1441\/revisions"}],"predecessor-version":[{"id":1517,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1441\/revisions\/1517"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=1441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=1441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=1441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}