Editing an html <div> inside a PHP statement

2749 views php
7

I'm coding a PHP script supposed to create some boxes inside a webpage. I'm having problems setting the background color of the box. Here is the part of code involved:

 <div class="container">
 //$color is declared at the top of my file
   <?php
    echo '<div >'
    ?>

</div>

So background-color should take the color from the $color variable, but i don't know how to make that PHP part inside the html statement. The error i get is syntax error, unexpected '?'

answered question

2 Answers

8

Please concate your color variable with . Delimiter

<?php
  echo '<div style="background-color: '.$color.'; width: 30px; height: 30px">'
?>

posted this
7

And another alternative:

<?php
    printf('<div style="background-color: %s; width: 30px; height: 30px">', $color);
?>

posted this

Have an answer?

JD

Please login first before posting an answer.