Jump to content

Recommended Posts

Posted

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

Posted

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.

  • Like 1
Posted

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.

Posted

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.

Posted

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>

Posted

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
  • 2 weeks later...
Posted

wolstech:

PHP is a server-side language, and not all code which isn't between <?php ?> is executed anyway.

 

Example:

<?php

function 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

Posted

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() ?>

Posted

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

  • 2 months later...
Posted

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...