What is the correct way to assemble interfaces in PHP pages? [closed]

1

In several projects I've seen there, where database queries are required to get information, and later build tables, I noticed the following approach presented in the code snippet below:

<?php
    while($row = mysql_fetch_array($res)) {
?>
    <tr>
        <td><?php echo $row['nome']; ?></td>
        <td><?php echo $row['idade']; ?></td>
        <td><?php echo $row['dia']; ?></td>
        <td><?php echo $row['turno']; ?></td>
    </tr>
<?php
    }
?>

In it, the person opens several times <?php ?> to extract information from the database, and mount the page.

This does not seem correct, would not it be better to use javascript (ajax) to retrieve information, and then DOM to mount the page?

My question is what would be the best way to build pages where you need to query databases for information.

    
asked by anonymous 14.02.2017 / 18:14

1 answer

1

There is nothing wrong with opening and closing the tags to compose HTML content.

Everything that is delimited by the PHP tag is parsed by the PHP compiler.

If you pass non-PHP stuff, this has an additional cost for the compiler to process the data.

Let's look at an example:

echo '<table><tr><td>'.$variavel.'</td></tr></table>';

Here in this example, PHP will only "spit" back the static excerpt <table><tr><td> and </td></tr></table> .

That is, it sent the compiler to process text that has nothing to render. It is static information. PHP had to read this information, increasing memory consumption and processing.

Otherwise,

<table><tr><td><?php echo $variavel;?></td></tr></table>

The compiler will only receive the $variavel; stretch.

What would be a bad practice, it's something like this

<?php
echo $variavel;
?>
<?php
echo $outra_variavel;

function alguma_coisa() {
    return null;
}

echo alguma_coisa();
?>

Here in this case, yes, we even have a problem because it closed and opened unnecessarily because the following code is also PHP. It could just continue in the same tag

<?php
echo $variavel;

echo $outra_variavel;

function alguma_coisa() {
    return null;
}

echo alguma_coisa();
?>

In addition, when doing

?>
<?php

It is generating an output that is usually the common cause among beginners with the famous "header already sent". But I will not go into this because it is more extensive and deviates from the focus of the question.

Escaping quotes

One difficulty when passing everything through PHP is having to escape characters:

echo '<tr><td>texto com aspa simples \''.$variavel.'</td></tr>';

It can be resolved using double quotation marks

echo "<tr><td>texto com aspa simples '".$variavel."</td></tr>";

But this also depends a lot on the project code pattern. Particularly I use single quotes and some people prefer double quotation marks. This is a matter of opinion and somewhat controversial. However, even with double quotes falling in the same trap when the static text has double quotation marks. You have to escape:

echo "<tr><td>texto com aspa dupla \"".$variavel."</td></tr>";

So far, these are very simple examples. In real life you will get things like this

lorem ipsum single quot'e double "quote"
<b>
<script>
var str = "foo'bar";
document.write(str);
</script>
<?php echo $variavel_do_php;?>
</b>

This here is still a child level ... but it is just to illustrate

See how it looks differently:

echo '
lorem ipsum single quot\'e double "quote"
<b>
<script>
var str = "foo\'bar";
document.write(str);
</script>
'.$variavel_do_php.'
</b>';

Do you say which one is better for reading code?

In terms of performance they make no difference. So I usually opt for what is simpler and readable and, of course, it all depends on the purpose of use and that's what matters most.

JavaScript Template

As for using JavaScript to mount the view, you should evaluate the project requirements. If it is a web project, usually a website has to be SEO compliant. If you mount the pages via JavaScript you will have problems with SEO because search engines do not interpret JavaScript.

Layout layout layout with JavaScript was heavily used in the past before the "Google Era". It was very common for websites to build content on pages with JavaScript because it greatly reduces the HTML code. Unfortunately all were forced to increase redundant content on pages due to SEO.

Currently it is used more for private projects, intranets or even websites that do not care about SEO.

MVC

If you analyze the question code for an MVC project, we can say that it is not a good practice because in MVC it does not normally invoke PHP functions directly in View. It is usually using a template engine, helpers, etc.

Summarizing

I have highlighted only obvious points. And there are many other points to mention but that makes the subject too long.

In general, avoid thinking about "what's best for X or Y?". See this question as "what is most appropriate for project X or situation Y?". For there is no such thing as "the universal solution." There are several solutions that best suit a particular situation.

Minimizing visual mess

For those who are uncomfortable with the use of <?php echo $variavel;?> , it is possible to reduce using short tag.

The equivalent of this <?php echo $variavel;?> can be written as <?=$variavel;?>

Remembering that there was a time when the elimination of the short tag was considered. At that time, as a precaution, I stopped using. A while later they went back and decided to keep it but I still continue to avoid use as a precaution. But that is personal choice. I do not want to say that you should think alike.

The use of <?php is recommended to avoid confusion with XML documents that also use <? as a tag.

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Tove</to>
  ....
    
14.02.2017 / 19:17