When or how to integrate php code into html

Asked

Viewed 106 times

0

I’m trying to make a simple application without any Framwork to put into practice some concepts I learned.

I studied a lot about back-end with an emphasis on POO, but when I went to index.php the questions came up:

  • And now?
  • Is that the way it is?
  • Do you have to summarize that part?

Question 1:

I read in some places that the right thing is to generate the values in Json to facilitate maintainability, but honestly never understood very well. I know what I must do in Json, I do it whenever possible but for me Json is just a bunch of keys we send to someone. Can you give me an example where people convert the Json in HTML?

Question 2:

The way I’m doing it is this:

<HTML>
    <HEAD>
        <?php
            require_once("autoload.php");
            $r = new objeto();
            $r->GerarNome("zezinho");
        ?>
    </HEAD>
    <BODY>
        <H1> <?php $r;//mostra "zezinho" ?> </H1>
    </BODY>
<HTML/>

I am loading the requires and creating objects in the head (In my case they are only given as names they will have in my application). The results are coming but I feel like I’m completely wrong, that feeling of not doing it the right way. I wanted to know if I’m doing well, if I’m completely wrong, if I have to do it differently.

If you have a new form, or something that you recommend me to study, you can tell me that I want to do everything possible for good practice!

  • 1

    The idea is to separate the responsibilities (what happens on the back and the front), so changes in one do not directly affect the other, usually the front is done with some framework like Vue, angular or React

2 answers

4


You are not wrong and you are not right. There are cases and cases.

I don’t particularly like using PHP to render HTML. In my real scenarios this made it difficult for me to maintain and I lost a little in performance. But, as I said, there are cases and cases. Come on.

Scenario without JSON (your):

PHP is running as DOM is rendered in the browser. This means that your user will only see the page after your code is processed.

Pros:

  • development becomes simpler and faster, if used correct standards.

Cons:

  • depending on what your code does, it can slow down.
  • Maintenance difficulty, as your front-end and back-end code will be mixed. (For example, suppose you decide to make a complete visual change in the application. You will have to deal with html code and be careful not to break the back end of the application, since this will be mixed)

Scenario with JSON (API Rest):

In this case, PHP will be responsible for processing the requests and delivering the JSON response so that you can asynchronously (AJAX) capture them on the front end. This means that DOM loading and HTTP request will be done separately.

Pros:

  • More vivid web applications
  • Easy maintenance, since the codes will be separate.
  • Easy to implement new applications using the same backend. (For example: you have a website and need to raise the mobile version of this site. You will already have the backend almost ready, just need to worry about the front-end)

Cons:

  • The development may get a little more complex, but it’s worth it. : D

There are many details that I could not explain here briefly, but I will give you the path of the stones: search by API Rest with PHP and ajax.

1

php was created to be integrated with html, but nowadays the less direct contact the two have will be better, so think of the MVC standard using a template system, like Twig for example.

I know you’re still learning, but this would be the best way for you, in my humble opinion.

Browser other questions tagged

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