syntax error, Unexpected 'endforeach' (T_ENDFOREACH), expecting end of file. Does anyone have any idea why this error?

Asked

Viewed 207 times

-2

<html>
<body>
<h1>Listagem de Produtos</h1>
   <table>
        <?php foreach($produtos as $p) ?>
     <tr>
        <td><?php $p->nome ?></td>
        <td><?php $p->valor ?></td>
        <td><?php $p->descricao ?></td>
        <td><?php $p->valor ?></td>
     </tr>
        <?php endforeach ?>
    </table>
</body>
</html>
  • 1

    Missed : in foreach($produtos as $p)

1 answer

3


<html>
<body>
<h1>Listagem de Produtos</h1>
   <table>
        <?php foreach($produtos as $p): ?>
     <tr>
        <td><?=$p->nome;?></td>
        <td><?=$p->valor;?></td>
        <td><?=$p->descricao;?></td>
        <td><?=$p->valor;?></td>
     </tr>
        <?php endforeach; ?>
    </table>
</body>
</html>

Try the code above. The code was missing : at the close of $p). I simplified your $p->valor.

To display a result of a variable on the screen you must use in PHP echo or print.

The simplification made in your code was the printing of the value in the table column <?=$variavel;?> displays the variable value directly on the screen.

More information about foreach in the PHP documentation

Browser other questions tagged

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