Jump to content

Php Code Not Showing In Final Webpage Output


wal

Recommended Posts

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>

 

<?php

echo "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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

 

<?php

echo "Hello World!";

?>

 

</body>

</html>

Link to comment
Share on other sites

Php ignores the html, but apache does not. Think of it like this:

 

1. User requests page

2. Apache finds the file requested

3. 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.

  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...

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:

<?php

function showDiv() {

echo "<div id="test">Hello, world!</div>";

} ?>

 

to use this, you can <? php showDiv() ?>

Link to comment
Share on other sites

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:

<?php

function 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

Link to comment
Share on other sites

  • 2 months later...

Php ignores the html, but apache does not. Think of it like this:

 

1. User requests page

2. Apache finds the file requested

3. 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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...