6

I have a small snippet of code that I want to style from echo.

foreach($months as $key => $month){
  if(strpos($filename,$month)!==false){
        echo '<style = "font-color: #ff0000"> Movie List for {$key} 2013 </style>';
    }
}

This is not working, and I've been looking over some resources to try to implement this. Basically I want font-family: Arial and font-size: 11px; and the font-color: #ff0000;

Any php assistance would be helpful.

2
  • You need to enclose the string in double quotes, not single quotes for variable interpolation
    – Phil Cross
    Jun 3, 2013 at 17:17
  • You need a ';' after style params.
    – cerd
    Jun 3, 2013 at 17:17

6 Answers 6

8
foreach($months as $key => $month){
  if(strpos($filename,$month)!==false){
        echo "<div style ='font:11px/21px Arial,tahoma,sans-serif;color:#ff0000'> Movie List for $key 2013</div>";
    }
}
2
  • Awesome. It works with this code and your other code, too. I'm gonna start using <div>. Not a fan of <span> Jun 3, 2013 at 17:43
  • Whatever problem you were having with <span> should affect <div>, too. Either they both get echoed or they both get interpreted.
    – Barmar
    Jun 3, 2013 at 19:02
4
echo "<span style = 'font-color: #ff0000'> Movie List for {$key} 2013 </span>";

Variables are only expanded inside double quotes, not single quotes. Since the above uses double quotes for the PHP string, I switched to single quotes for the embedded HTML, to avoid having to escape the quotes.

The other problem with your code is that <style> tags are for entering CSS blocks, not for styling individual elements. To style an element, you need an element tag with a style attribute; <span> is the simplest element -- it doesn't have any formatting of its own, it just serves as a place to attach attributes.

Another popular way to write it is with string concatenation:

echo '<span style = "font-color: #ff0000"> Movie List for ' . $key . ' 2013 </span>';
6
  • I appreciate the information, but it still echo's everything from <span> ... </span> Jun 3, 2013 at 17:28
  • That's what it's supposed to do. The browser shold interpret it as HTML. If not, it seems like you have a server configuration problem, preventing it from sending the correct Content-type: text/html header.
    – Barmar
    Jun 3, 2013 at 17:30
  • Its not working... my code :: <?php echo "<span style = 'font-color: #ff0000'> $_product->getData('ayyappa_coupon_code_offer'); </span>" ?>
    – Gem
    Nov 17, 2017 at 9:17
  • @Rathinam You need curly braces if you're not substituting a simple variable. {$_product->getData('ayyappa_coupon_code_offer')}
    – Barmar
    Nov 17, 2017 at 16:52
  • @Barmar This my code still not applied font color : <?php echo "<span style = 'font-color: #ff0000'> {$_product->getData('ayyappa_coupon_code_offer')} </span>";?>
    – Gem
    Nov 21, 2017 at 5:19
2

Are you trying to echo out a style or an inline style? An inline style would be like

echo "<p style=\"font-color: #ff0000;\">text here</p>";
3
  • this is what i'm trying to do, however, it still echo's the code, <p style="font-color: #ff0000;">text here</p> Maybe I'm doing something incorrectly? Jun 3, 2013 at 17:25
  • When you say it's still echoing out the code, is it echoing out the code for all php on the page or just this segment?
    – Julie Beck
    Jun 4, 2013 at 0:23
  • I figured it out. Your code was correct as well. I had a small mistake but worked my way through it. thank you! Jun 4, 2013 at 3:38
2
echo '< span style = "font-color: #ff0000"> Movie List for {$key} 2013 </span>';
2
 echo "<a href='#' style = \"font-color: #ff0000;\"> Movie List for {$key} 2013 </a>";
1
  • Still not working. It echo's the entire code for that line. :/ Jun 3, 2013 at 17:17
1

You should also use the style 'color' and not 'font-color'

<?php

foreach($months as $key => $month){
  if(strpos($filename,$month)!==false){
        echo "<style = 'color: #ff0000;'> Movie List for {$key} 2013 </style>";
    }
}

?>

In general, the comments on double and single quotes are correct in other suggestions. $Variables only execute in double quotes.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.