0

I need to set a div's background colour using PHP. Here's what I'm doing at the moment:

<div class="box" style="background-color:"<?php echo $permacolour; ?>"">

However, this isn't working. What am I doing wrong?

Thanks

5 Answers 5

6
<div class="box" style="background-color:<?=$permacolour?>">

quotes do not belong to PHP syntax. PHP tags are <?, <?php and ?> only

Sign up to request clarification or add additional context in comments.

4 Comments

Good answer, but I'd note that <?php ?> is a perfectly acceptable syntax for this, OP doesn't have to use <?= ?> - it's the quotes, not the PHP tags, that are the problem.
Also, the <?= in this example is a short hand way of writing <?php echo
And requires short tags be enabled in PHP's ini file! :)
@Ascherer Nice. Good to know. Fingers crossed I'll start seeing less Smarty use as a result.
4
style="background-color:"<?php echo $permacolour; ?>""

You have two sets of quotes here. Try this:

style="background-color:<?php echo $permacolour; ?>"

Comments

4

To be near your original post, here's the correct way :

<div class="box" style="background-color: <?php echo $permacolour; ?>">

The problem is that you were writing the background color surrounded by double quotes. The result would have been like

<div class="box" style="background-color:"red"">

instead of

<div class="box" style="background-color: red">

So just remove the double quotes between the value you are good to go.

Of course, this suppose you defined $permacolour or you made sure the value of $permacolour is sanitized and filtered if inputed by a user.

The answer of @your-common-sense is based on the shorthand syntax that is not always activated on hosted servers.

1 Comment

Worth noting, Moving forward in php, after 5.4, <?= will always be available, regardless of the short_tag setting
0
<div class="box" style="background-color:<?php echo $permacolour; ?>;">

Comments

0

remove the double quotes

style="<?php echo $permacolour ; ?>"

or without quotes at all would be easier for some versions in html

style=<?php echo $permacolor ;?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.