Questions & Answers

Using a $variable for color in php?

I tried this, but doesn't work:

$color is taken from a database as Char #cccccc; (this part works)

'<span style="color:<?php $color ?>"><?php echo "###"?></span>'

I'm a beginner. I can store the hex #cccccc on a database and echo it out.

2023-01-22 23:01:13
You need to echo it. So echo $color
2023-01-22 23:01:13
if thats echo '<span style="color:<?php $color ?>"><?php echo "###"?></span>'; please include it in the question, so answers are not wrong
2023-01-22 23:01:13
it's not working. When I echo $color out the result is: #cccccc . Do I need to convert this string to use it as a hexadecimal number?
2023-01-22 23:01:13
fixed it.. your answer was correct! I made a stupid mistake in the database settings
2023-01-22 23:01:13
"but doesn't work" - what does that mean? What exactly is not working?
Answers(1) :

I have found 2 mistakes.

  1. You didn't use echo to print the value of color.
  2. You have not closed the opening tag correctly.

here is the working code.

<span style="color:<?php echo $color; ?>">
  Welcome to PHP <?php echo $color ?>