Oscar Reyes
Oscar Reyes

Reputation: 4352

how to really include html codes inside php

theres something i would like to try and has to do with including html on php, my code would be like this.

<?php
foreach($element as $data)
{
    <div class="view">
    echo $data;
    </div>
}
?>

that would be just an example, of course it will give me error like not expected error.

i just would like to make a this function for each element in array thats in $element but it has to be repeated with the class on css style. how to do?

sorry im new at php

Upvotes: 0

Views: 55

Answers (4)

hammus
hammus

Reputation: 2612

<?php 
foreach($element as $data){
    echo "<div class='view'>";
    echo $data;
    echo "</div>";
}
?>

OR:

<?php foreach($element as $data){?>
     <div class='view'>
        <?php echo $data; ?>
     </div>
<?php } ?>

Upvotes: 0

Mahmood Rehman
Mahmood Rehman

Reputation: 4331

Try like this :

<?php foreach($element as $data){ ?>
  <div class="view">
   <?php echo $data; ?>
  </div>
<?php } ?>

Upvotes: 0

Phil
Phil

Reputation: 165062

Alternatively

<?php foreach ($element as $data) : ?>
<div class="view">
    <?= htmlspecialchars($data) ?>
</div>
<?php endforeach ?>

Upvotes: 4

Sibiraj PR
Sibiraj PR

Reputation: 1481

Two way is there

<?php foreach($element as $data) { ?>
  <div class="view">
   <?php echo $data; ?>
  </div>
<?php } ?>

OR

<?php foreach($element as $data) { 
  echo '<div class="view">';
    echo $data;
  echo '</div>';
  } ?>

Upvotes: 1

Related Questions