{"id":127,"date":"2017-04-21T19:14:28","date_gmt":"2017-04-21T19:14:28","guid":{"rendered":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/chapter\/chapter-26-php-output\/"},"modified":"2019-06-17T14:13:25","modified_gmt":"2019-06-17T14:13:25","slug":"chapter-26-php-output","status":"publish","type":"chapter","link":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/chapter\/chapter-26-php-output\/","title":{"raw":"Chapter 26: PHP Output","rendered":"Chapter 26: PHP Output"},"content":{"raw":"<div class=\"Basic-Text-Frame\">\r\n<h1 class=\"Section\">Print, Echo<\/h1>\r\n<div class=\"bc-section section\">\r\n<p class=\"BodyFirst\">PHP allows for two methods of sending output to the screen. One is called print, the other echo. While they provide the same functionality, echo is a construct (it will be treated as a command), where print is an expression (it will be evaluated and will return a value). When we use print or echo in PHP, both can be used as constructs (called without parenthesis after them, as if they are a command) or as a function (called with parenthesis like a function call). Print will return a 1 as a result when it is used, while echo will not return anything.<\/p>\r\nUltimately, the differences between print and echo are negligible. Debates over which to use range from consistency of verbiage, speed of processing (print technically takes four operating commands to echo\u2019s three as it has one more step of returning the 1), and obscure examples of where one might win out over the other.\r\n\r\nFor our examination here, use what you like. In extreme examples, a high volume of echos will be faster than a high volume of print statements, but there are far more important places to consider refining code for speed than any gains you might find here.\r\n\r\nTo send output to the screen, we can start with the famous example of Hello, World!\r\n<pre>&lt;?php echo \"Hello, World!\"; ?&gt;<\/pre>\r\nWe can also wrap the string in parenthesis as we discussed above if you feel it makes things more clear:\r\n<pre>&lt;?php echo (\"Hello, World!\"); ?&gt;<\/pre>\r\nCongratulations, you have just created your first PHP web page!\r\n\r\nWe can get a little more in depth before moving on with another example. When functions return something to us, we usually save that value and then take action on it. We can also send the output directly to the screen if we know it is formatted how we want to view it. The phpinfo() function for example gives us access to all the details about our server. If we use it without requesting a specific piece of information, it defaults to returning a full web page with all the details about our server. We can see this by using the following:\r\n<pre>&lt;?php echo phpinfo(); ?&gt;<\/pre>\r\nThis screen shot is just a portion of the entire response, which is usually several pages long. Keep this function in mind, as it is a convenient way of finding the settings of your server without digging into your config files.\r\n\r\n<span class=\"Picture-2\"><img class=\"alignnone\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1755\/2017\/04\/21191426\/chap26_echo_fmt.png\" alt=\"Screen shot of a portion of a response of a PHP page\" width=\"400\" height=\"343\" \/><\/span>\r\n<div class=\"textbox exercises\">\r\n<h3 class=\"learn-h\"><a>Learn more<\/a><\/h3>\r\n<p class=\"learn-m\">Keywords, search terms: Troubleshooting php, common programming errors<\/p>\r\n<p class=\"learn-m\">Common Programming Mistakes: <a href=\"http:\/\/www.dummies.com\/how-to\/content\/troubleshooting-a-php-script.html\"><span class=\"Hyperlink\">http:\/\/www.dummies.com\/how-to\/content\/troubleshooting-a-php-script.htm<\/span><\/a><span class=\"Hyperlink\">l<\/span><\/p>\r\n<p class=\"learn-m\">Debugging with Print and Eclipse: <a href=\"http:\/\/www.ibm.com\/developerworks\/library\/os-debug\"><span class=\"Hyperlink\">http:\/\/www.ibm.com\/developerworks\/library\/os-debug<\/span><\/a><span class=\"Hyperlink\">\/<\/span><\/p>\r\n\r\n<\/div>\r\n<\/div>\r\n<\/div>","rendered":"<div class=\"Basic-Text-Frame\">\n<h1 class=\"Section\">Print, Echo<\/h1>\n<div class=\"bc-section section\">\n<p class=\"BodyFirst\">PHP allows for two methods of sending output to the screen. One is called print, the other echo. While they provide the same functionality, echo is a construct (it will be treated as a command), where print is an expression (it will be evaluated and will return a value). When we use print or echo in PHP, both can be used as constructs (called without parenthesis after them, as if they are a command) or as a function (called with parenthesis like a function call). Print will return a 1 as a result when it is used, while echo will not return anything.<\/p>\n<p>Ultimately, the differences between print and echo are negligible. Debates over which to use range from consistency of verbiage, speed of processing (print technically takes four operating commands to echo\u2019s three as it has one more step of returning the 1), and obscure examples of where one might win out over the other.<\/p>\n<p>For our examination here, use what you like. In extreme examples, a high volume of echos will be faster than a high volume of print statements, but there are far more important places to consider refining code for speed than any gains you might find here.<\/p>\n<p>To send output to the screen, we can start with the famous example of Hello, World!<\/p>\n<pre>&lt;?php echo \"Hello, World!\"; ?&gt;<\/pre>\n<p>We can also wrap the string in parenthesis as we discussed above if you feel it makes things more clear:<\/p>\n<pre>&lt;?php echo (\"Hello, World!\"); ?&gt;<\/pre>\n<p>Congratulations, you have just created your first PHP web page!<\/p>\n<p>We can get a little more in depth before moving on with another example. When functions return something to us, we usually save that value and then take action on it. We can also send the output directly to the screen if we know it is formatted how we want to view it. The phpinfo() function for example gives us access to all the details about our server. If we use it without requesting a specific piece of information, it defaults to returning a full web page with all the details about our server. We can see this by using the following:<\/p>\n<pre>&lt;?php echo phpinfo(); ?&gt;<\/pre>\n<p>This screen shot is just a portion of the entire response, which is usually several pages long. Keep this function in mind, as it is a convenient way of finding the settings of your server without digging into your config files.<\/p>\n<p><span class=\"Picture-2\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/1755\/2017\/04\/21191426\/chap26_echo_fmt.png\" alt=\"Screen shot of a portion of a response of a PHP page\" width=\"400\" height=\"343\" \/><\/span><\/p>\n<div class=\"textbox exercises\">\n<h3 class=\"learn-h\"><a>Learn more<\/a><\/h3>\n<p class=\"learn-m\">Keywords, search terms: Troubleshooting php, common programming errors<\/p>\n<p class=\"learn-m\">Common Programming Mistakes: <a href=\"http:\/\/www.dummies.com\/how-to\/content\/troubleshooting-a-php-script.html\"><span class=\"Hyperlink\">http:\/\/www.dummies.com\/how-to\/content\/troubleshooting-a-php-script.htm<\/span><\/a><span class=\"Hyperlink\">l<\/span><\/p>\n<p class=\"learn-m\">Debugging with Print and Eclipse: <a href=\"http:\/\/www.ibm.com\/developerworks\/library\/os-debug\"><span class=\"Hyperlink\">http:\/\/www.ibm.com\/developerworks\/library\/os-debug<\/span><\/a><span class=\"Hyperlink\">\/<\/span><\/p>\n<\/div>\n<\/div>\n<\/div>\n\n\t\t\t <section class=\"citations-section\" role=\"contentinfo\">\n\t\t\t <h3>Candela Citations<\/h3>\n\t\t\t\t\t <div>\n\t\t\t\t\t\t <div id=\"citation-list-127\">\n\t\t\t\t\t\t\t <div class=\"licensing\"><div class=\"license-attribution-dropdown-subheading\">CC licensed content, Shared previously<\/div><ul class=\"citation-list\"><li>The Missing Link. <strong>Authored by<\/strong>: Michael Mendez. <strong>Provided by<\/strong>: Open SUNY Textbooks. <strong>Located at<\/strong>: <a target=\"_blank\" href=\"https:\/\/textbooks.opensuny.org\/the-missing-link-an-introduction-to-web-development-and-programming\/\">https:\/\/textbooks.opensuny.org\/the-missing-link-an-introduction-to-web-development-and-programming\/<\/a>. <strong>License<\/strong>: <em><a target=\"_blank\" rel=\"license\" href=\"https:\/\/creativecommons.org\/licenses\/by-nc-sa\/4.0\/\">CC BY-NC-SA: Attribution-NonCommercial-ShareAlike<\/a><\/em><\/li><\/ul><\/div>\n\t\t\t\t\t\t <\/div>\n\t\t\t\t\t <\/div>\n\t\t\t <\/section>","protected":false},"author":311,"menu_order":5,"template":"","meta":{"_candela_citation":"[{\"type\":\"cc\",\"description\":\"The Missing Link\",\"author\":\"Michael Mendez\",\"organization\":\"Open SUNY Textbooks\",\"url\":\"https:\/\/textbooks.opensuny.org\/the-missing-link-an-introduction-to-web-development-and-programming\/\",\"project\":\"\",\"license\":\"cc-by-nc-sa\",\"license_terms\":\"\"}]","CANDELA_OUTCOMES_GUID":"","pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[],"contributor":[],"license":[],"class_list":["post-127","chapter","type-chapter","status-publish","hentry"],"part":121,"_links":{"self":[{"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/pressbooks\/v2\/chapters\/127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/wp\/v2\/users\/311"}],"version-history":[{"count":4,"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/pressbooks\/v2\/chapters\/127\/revisions"}],"predecessor-version":[{"id":344,"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/pressbooks\/v2\/chapters\/127\/revisions\/344"}],"part":[{"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/pressbooks\/v2\/parts\/121"}],"metadata":[{"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/pressbooks\/v2\/chapters\/127\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/wp\/v2\/media?parent=127"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/pressbooks\/v2\/chapter-type?post=127"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/wp\/v2\/contributor?post=127"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/courses.lumenlearning.com\/suny-the-missing-link-an-introduction-to-web-development-and-programming\/wp-json\/wp\/v2\/license?post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}