wal Posted August 21, 2014 Posted August 21, 2014 Hi Members, I'm new to PHP and just finished creating a new file to start coding. I understand Heliohost supports PHP. I created a test file and coded the following simple code. <!DOCTYPE html><html><body> <h1>My first PHP page</h1> <?phpecho "Hello World!";?> </body></html> Upon viewing the final output page, all that is showing is, "My first PHP page". I do not see the additional, "Hello World" which is the PHP script. Why is it not being displayed ?? Thank you Quote
wolstech Posted August 21, 2014 Posted August 21, 2014 Did you put a .php extension on the file? Files that end in .html won't be processed for PHP code. The script itself is fine. 1 Quote
wal Posted August 21, 2014 Author Posted August 21, 2014 You are correct! I changed the file name from 'index.html' to 'index.php' and the PHP code output is now displayed. I thought if I changed the name to anything other than 'index.html' then perhaps the html code would not be displayed. In other words, how is the html code being displayed with a PHP file extension? The code shown in my first post includes both HTML and PHP. Yes, I'm a newbie at programming therefore please be patient. Thank you kindly. Quote
wolstech Posted August 21, 2014 Posted August 21, 2014 Only the code between <?php and ?> tags is processed as php code. Everything else in the file is ignored by php and treated as if it were not in a .php file. Quote
wal Posted August 26, 2014 Author Posted August 26, 2014 Ok but it still does not answer my question as to why the file extension "index.php" is displaying the HTML part of the code output which reads "My first PHP page". If the file extension ,php only processes php code and ignores everything else then why did it print the html output that reads "My first PHP page"? Displayed below I have both HTML and PHP code in the same file with the file name "index.php". Thank you kindly. <!DOCTYPE html><html><body> <h1>My first PHP page</h1> <?phpecho "Hello World!";?> </body></html> Quote
wolstech Posted August 26, 2014 Posted August 26, 2014 Php ignores the html, but apache does not. Think of it like this: 1. User requests page2. Apache finds the file requested3. Apache feeds that file to php.4. Php runs all php code in the file and replaces it (in memory, the file isn't actually modified) with whatever that code outputs.5. Processed file is given back to apache (data now contains all HTML, since the php was run).6. Apache sends to user. 2 Quote
raffahacks Posted September 3, 2014 Posted September 3, 2014 wolstech:PHP is a server-side language, and not all code which isn't between <?php ?> is executed anyway. Example:<?phpfunction showDiv() {?><div id="test">Hello, world!</div><?php } ?> If showDiv isn't called from PHP code, that code will never be shown, even if it's not in PHP code Quote
hussam Posted September 3, 2014 Posted September 3, 2014 showDiv() is a php function in your case.but it doesn't do anything apart from show html code. <div> is an html tag so you close the php tag before <div ...> and then added <?php } ?> for the closing }. to use your showDiv function, you need to <?php showDiv() ?> if this confuses you, there is always:<?phpfunction showDiv() {echo "<div id="test">Hello, world!</div>"; } ?> to use this, you can <? php showDiv() ?> Quote
raffahacks Posted September 4, 2014 Posted September 4, 2014 showDiv() is a php function in your case.but it doesn't do anything apart from show html code. <div> is an html tag so you close the php tag before <div ...> and then added <?php } ?> for the closing }. to use your showDiv function, you need to <?php showDiv() ?> if this confuses you, there is always:<?phpfunction showDiv() {echo "<div id="test">Hello, world!</div>"; } ?> to use this, you can <? php showDiv() ?>It was just an example to say that some code which isn't officially in php code Quote
wal Posted November 20, 2014 Author Posted November 20, 2014 Php ignores the html, but apache does not. Think of it like this: 1. User requests page2. Apache finds the file requested3. Apache feeds that file to php.4. Php runs all php code in the file and replaces it (in memory, the file isn't actually modified) with whatever that code outputs.5. Processed file is given back to apache (data now contains all HTML, since the php was run).6. Apache sends to user. Hi Wolstech, Interesting! Thanks for the explanation. What search terms would I best use in google to read more about your reply and how Apache displays and sends web pages to users? Read the "Apache wiki" but its a bit complex. Perhaps I'm not utilizing the correct search terms. Still googling information. Thank you kindly Quote
wal Posted November 20, 2014 Author Posted November 20, 2014 Ok Found it! Further searching resulted in the following links that explain how the web server (Apache) processes the request (from a client i.e Firefox, Chrome web browsers)) to deliver/load a web page via a network. The first 5 links listed are on-topic. Also listed are other links referencing historical aspects of the internet and internet related. Excellent reading. 1) http://en.wikipedia.org/wiki/Web_server2) http://en.wikipedia.org/wiki/Client_%28computing%293) http://en.wikipedia.org/wiki/Apache_HTTP_Server4) http://en.wikipedia.org/wiki/Server-side_scripting5) http://en.wikipedia.org/wiki/Computer_network6) http://en.wikipedia.org/wiki/Comparison_of_web_server_software7) http://en.wikipedia.org/wiki/Web_hosting_service8) http://en.wikipedia.org/wiki/Virtual_hosting9) http://en.wikipedia.org/wiki/History_of_the_web_browser10) http://en.wikipedia.org/wiki/.com11) http://en.wikipedia.org/wiki/.org12) http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains13) http://en.wikipedia.org/wiki/Domain_Name_System14) http://en.wikipedia.org/wiki/Internet15) http://en.wikipedia.org/wiki/Outline_of_the_Internet16) http://en.wikipedia.org/wiki/Intranet17) http://en.wikipedia.org/wiki/Index_of_Internet-related_articles Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.