When or how to integrate a php code into html

0

I'm trying to make a simple application with no Framwork to put into practice some concepts I've learned.

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

  • What now?
  • Is it anyway?
  • Do you have to summarize this part?

Question 1:

I've read in some places that it's okay to generate the values in Json to make it easier to maintain, but honestly, I never quite understood. I know what I should do in Json , I do whenever possible but for me Json is just a bunch of keys that we send to someone. Can you give me an example where people convert Json to HTML ?

Question 2:

The way I'm doing it is as follows:

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

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

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

    
asked by anonymous 08.07.2018 / 14:54

2 answers

3

You're not wrong or right. There are cases and cases.

I do not particularly like using PHP to render HTML. In my real scenarios this made my maintenance difficult and I lost a bit in performance. But, as I said, there are cases and cases. Come on.

Scenario without JSON (yours):

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

Pros :

  • development is simpler and faster, if you use the correct patterns.

Cons :

  • Depending on what your code does, it can cause slowness.
  • Difficulty in maintenance, since your front-end and back-end code will be mixed. (For example, suppose you decide to make a complete change of look in the application. You will have to deal with the html code and be careful not to break the backend of the application, as this will be mixed.)

Scenario with JSON (API Rest):

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

Pros:

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

Cons:

  • Development may get a bit more complex, but it's worth it. : D

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

    
08.07.2018 / 15:36
0

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

I know you're still learning, but that would be the best path for you, in my humble opinion.

    
09.07.2018 / 04:42