Change HTML document to PHP

Asked

Viewed 3,455 times

1

Guys, how do I turn an HTML file to PHP? Just change the extension? Is that right? Apparently, my server read the page I switched from HTML to PHP, but I have doubts whether it’s right or not, after all, I just changed the extension to .html for .php.

  • 1

    You need to have apache installed on your server otherwise the php code will become text at the time of display.

  • 1

    just change the html extension to php, since your server runs php, no problem at all.

  • 1

    @rray if there is no apache both php code and html also becomes text, right?

  • The HTTP responses generated by your Phps can be anything, like: json, image, html, txt, etc. PHP is not the "equivalent" to HTML, it is only possible to write HTML in the middle of it that will be processed and sent to via download as if it were a pure HTML, after processing.

5 answers

3


Mikaela, imagine you have a file called index.html with the content described below:

<!DOCTYPE html>
<html>
    <head>
        <title>Aprendendo HTML e PHP</title>
    </head>

    <body>

        <p>Prazer, me chamo Godfrey the King.</p>

    </body>
</html>

You as a curious person, decided to change the extension to .php.

When opening the page in your browser - remembering that you need to have a web server installed on your computer, which is easily installed using XAMPP, WAMP, Easyphp, among others. - the following sentence shall appear::

"Prazer, me chamo Godfrey the King."

If you change the code a little, to:

<!DOCTYPE html>
<html>
    <head>
        <title>Aprendendo HTML e PHP</title>
    </head>

    <body>

        <?php $nome = "Mikaela"; ?>

        <p>Prazer, me chamo <?php echo $nome; ?>.</p>

    </body>
</html>

The phrase will be exchanged for: "Prazer, me chamo Mikaela.".

Now do a short test and change the file extension to .html again and reload the page.

Noticed the difference?


HTML is a markup language used in the construction of Web. HTML documents can be interpreted by browsers.

PHP is a free interpreted language, originally used only for the development of applications present and acting on the server. PHP scripts are interpreted on the server side.

  • yes, that I understood, I will detail a little more... I created a site in html, all of it in Html5 and css3; as I am learning, I decided to add a login and password in it, with database, ie add php and mysql, I have the xampp server on my computer, and the only thing I did was change the ending of my html file to php, I downloaded it from localhost, and it opened the php file normally, the same way it opened in html, I haven’t added anything in php yet, the question is, will give problem on the site at the time I host it online if I leave it so?

  • 1

    @Mikaelakleinkauf No, it won’t. What you can’t, is to forget to put the code PHP within the tag <?php ?> - for example - when you write code PHP.

  • As I said in my reply, all you write inside the tags PHP will be interpreted by your web server (in your case the apache) and so "returned" to your browser to display the data.

  • 1

    @Mikaelakleinkauf If I’ve cleared your doubts, don’t forget to mark the answer as correct.

  • yes yes, that was my question very muuuuiiittoooo thanks =D

  • 1

    @Mikaelakleinkauf So if possible, accept my answer as correct. https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta

Show 1 more comment

0

It’s just doing this, changing to php makes your server understand that that page has code in php. If you try, for example, to run a php code with a *.html file, nothing will happen, just print a text on the screen with your code.

0

The change of extension will only present problem if the page needs to execute some instruction in PHP.

Code page example that will have no problem in both extensions:

<h1>Hello World!</h1>

The "Hello World!" below will only be presented if you have the extension at PHP:

<h1><?php echo 'Hello World!'; ?></h1>

So that the code PHP run must be on a web server prepared, such as Apache for example.

  • That’s my question, I have apache installed, and I changed only the html extension to php; on my localhost, it displayed exactly the same, the same way it displayed with html ending, I have not yet inserted anything in php, but I will put php inside the document, I’ll have to put echo throughout the document?

  • @Mikaelakleinkauf No. The echo just print text on the screen inside the PHP code <?php ?>. Any other text on screen vc makes it equal to an HTML file.

  • then my html document will work fine if I switch to php?

  • If the page will have codes on PHP it is necessary to be in this extension. In the places that are fixed contents there is no need to put the echo, can leave plain text inside the HTML tag. It will work correctly.

0

To change just change the file extension, don’t forget to put the . php file on an Apache server (you can use xampp, wampp... for testing)

0

First it is necessary to understand what is front-end and back-end, these questions should help:

So that’s how it works, your machine/browser requests via HTTP a page on your site, PHP does not run on your machine, it is a script that generates an HTTP response and sends via download to your corresponding request machine, something like these illustrations:

http

and

http

In other words, PHP processes a response that is an HTML and depending on the server configuration the extension .php nor will it exist, this is just a way the language uses to facilitate the creation of pages.

The HTTP responses generated by your Phps can be anything, like: json, image, html, txt, etc.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.