0

I want to change the colour of some text and it has to be inside double quotation marks as it is a variable. This is the only instance in which it happens so I won't bother with CSS.

  echo "$playername ";  

I tried

echo "<font color="yellow"> $playername </font>" ;

But it is not working, I assume this is because it does not like double quotes inside double quotes. Any help would be much appreciated.

  • `echo " $playername " ;` or `echo ' $playername ' ;` – Pranav C Balan Mar 19 '19 at 18:56
  • 2
    There's so many ways to solve this. I know this is a dupe. Just gotta find the right one. – John Conde Mar 19 '19 at 18:56
  • 1
    Side note, `` doesn't exist anymore so don't use it – j08691 Mar 19 '19 at 18:58
  • Not all attributes need to be quoted. (XHTML is not a thing anymore). `"…"` also would do. (Though again, `` is very outdated.) – mario Mar 19 '19 at 19:04
  • just try: echo ' $playername '; – bill.gates Mar 19 '19 at 19:06
  • 1
    Double quotes inside double quotes break your string, so you must use escape character like backslash for double quotes \" Also change font and color to span and style. $playername is variable so need to use concatenation operator (.) to add it to string. echo "".$playername.""; – JarekBaran Mar 19 '19 at 19:08
  • I'd like to advocate reopening this question because it is not simply a PHP question. It raises questions about what is valid HTML as pertaining to attributes as well as the FONT tag. – slevy1 Mar 19 '19 at 19:43

1 Answers1

0

The necessity of quoted attribute values only comes into play when dealing with values that may contain contain whitespace or characters meaningful to HTML, such as "<". So, in this case, the OP does not need to quote the attribute value; see here.

If one insists on quoting an attribute value, one may use single quotes or else quote double quotes as in below example:

<?php 
$playername = "Nobody";
$yellow="\"yellow\"";
echo "<font color=$yellow>$playername</font>";

Note: while the FONT tag is obsolete, it nonetheless works at any rate in my version of Google Chrome (YMMV). Note, the MDN adamantly discourages using the FONT tag:

Do not use this element! Though once normalized in HTML 3.2, it was deprecated in HTML 4.01, at the same time as all elements related to styling only, then obsoleted in HTML5.

slevy1
  • 3,797
  • 2
  • 27
  • 33