Code doubt

Asked

Viewed 68 times

-1

Could someone explain to me why the loop isn’t working, it used to work and the agr to breaking the head without understanding anything, it seems not being interpreted I don’t know. The "hello world" above works, but when to interact with HTML PHP does not work.

<?='hello world'?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Portal Noticias</title>
</head>
<body>
    <header>
        <div id="titulo">
            Portal Noticias
        </div>
    </header>

    <div id="noticias">
        <?for ($i=0; $i < 5; $i++) { ?>
            <article class="artigo">
                <h1>Titulo da noticia</h1>
                <p>
                    Lorem ipsum dolor sit amet, bla bla bla, consecetur. Lorem ipsum bla bla.
                </p>
            </article>
        <?}?> 

    </div>

</body>
</html>
  • Try to use <?php instead of <?.

1 answer

3

I modified your code in PHP, so you can see the answer more easily. But nothing prevents you from using it your way. But to simplify the explanation, I put it as follows.

The tag <?php is the default for PHP file openings, unless Short Tag is enabled, which allows you to open PHP with <?. Already the tag <?= It’s quite simple what she wants instead of using <?php echo $variable; ?> just use <?=$variable?>. In order to make the code cleaner and readable. Reference: Sopt - juniorb2ss

You can see a little more about the language in: PHP

In order to be able to use this loop within your HTML, the following lines are required:

    <?php
        for ($i=0; $i < 5; $i++) { 
             echo '
                <article class="artigo">
                    <h1>Titulo da noticia</h1>
                    <p>
                        Lorem ipsum dolor sit amet, bla bla bla, consecetur. Lorem ipsum bla bla.
                    </p>
                </article>
            ';
       }
   ?>

Remember not to leave a space after <?php, this can result in future problems. It is also important to note that if the content is dynamic, it is of great importance that you use Xmlhttprequest.

  • 1

    To complete, explaining that ?<?=? is just an abbreviation of ?<? php echo'. Leaves the code cleaner.

  • @Exact juniorlike.

Browser other questions tagged

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