Using html inside double quoted echos [duplicate] - php

This question already has answers here:
Double quotes within php script echo
(6 answers)
Echo double quotes inside double quotes issue [duplicate]
(2 answers)
PHP quotes inside quotes
(3 answers)
How to use single quote inside an echo which is using single quote
(7 answers)
Closed 3 years ago.
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.

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.

Related

How to fix double/single quotes in PHP [duplicate]

This question already has answers here:
How to work with double quotes and single quotes in php [duplicate]
(2 answers)
Closed 3 years ago.
this should be an easy question but im finding it hard to get a working answer, i am using Sendgrid php api and it requires that i do the following code in this layout.
in my code i have:
//this part is the sendgrid part:
$email->addContent(
"text/html", "//i need to put the bottom image code into here"
<?php echo '<img src="data:image/png;base64,'.base64_encode($portal['image']).'"width="1024px" height="768px"/>'; ?>
the end result should look something like:
$email->addContent(
"text/html", "<img src="data:image/png;base64,'.base64_encode($portal['image']).'"width="1024px" height="768px"/>"
i cant figure out how to use the correct quotes so that this code will work since it uses double and single quotes inside of eachother
You have to match the starting and ending quotes for a string. You're starting the first string with ", but then ending it with ' before the concatenation ..
If you want to put double quotes inside the string, use single quotes around it, and vice versa. If you need to include the same kind of quote inside the string as you use two create it, you have to escape the inner one.
$email->addContent(
"text/html", '<img src="data:image/png;base64,'.base64_encode($portal['image']).'" width="1024px" height="768px"/>'

Problems doing simple echo [duplicate]

This question already has answers here:
Escaping/encoding single quotes in JSON encoded HTML5 data attributes
(2 answers)
Double quotes within php script echo
(6 answers)
Closed 4 years ago.
Well, i'm trying to echo some lines of code but it uses both "" and ''.
As far as I know if you start a echo with "", everytime you use "" it will stop the echo.
How do I print this line of code without breaking it?
This is what I currently have :
echo "<a class='lightbox' href='img/projects/generic/project-16.jpg' data-plugin-options='{'type':'image', 'mainClass': 'mfp-with-zoom', 'zoom': {'enabled': true, 'duration': 300}}'>";
The data-plugin-options='{'type':'image', 'mainClass': 'mfp-with-zoom', 'zoom': {'enabled': true, 'duration': 300}}' is the one that is causing me trouble becouse i cant use "" neither can i use '', becouse one will break the echo, the other one will break the tag.
Hope any of you can help me understand and solve this small problem, big thanks in advance !
Have you tried something like this?
echo "<a class=\"lightbox\" href=\"img/projects/generic/project-16.jpg\" data-plugin-options=\"{'type':'image', 'mainClass': 'mfp-with-zoom', 'zoom': {'enabled': true, 'duration': 300}}\">";
Prefixing double quote in double quoted string with backslash (\") will insert it into the string itself and prevent it from stopping the string block.
Edit: Taking #Barmar's comment into account, it should be more like this to keep data-plugin-options's data as valid JSON, thus enclosing it in single quotes:
echo "<a class=\"lightbox\" href=\"img/projects/generic/project-16.jpg\" data-plugin-options='{\"type\":\"image\", \"mainClass\": \"mfp-with-zoom\", \"zoom\": {\"enabled\": true, \"duration\": 300}}'>";
So to sum it up, PHP does not support escaping double quotes with "", \" needs to be used instead. (analogically \' has to be used for single quotes strings)
The quotes in the data-plugin-options need to be double quotes, since it's parsed as JSON.
You can escape " inside a string delimited with " by preceding it with \.
echo "<a class='lightbox' href='img/projects/generic/project-16.jpg' data-plugin-options='{\"type\":\"image\", \"mainClass\": \"mfp-with-zoom\", \"zoom\": {\"enabled\": true, \"duration\": 300}}'>";

How to work with double quotes and single quotes in php [duplicate]

This question already has answers here:
Escaping quotation marks in PHP
(7 answers)
Closed 5 years ago.
I just wonder how people work with double and single quotes. I have a code where i got a little problem.
If i for example got this code:
<img src="img_fjords.jpg" onclick="document.getElementById('someID').style.display='block'" class="w3-hover-opacity">
Now i got no problem, but if im going to put this inside a echo "" then i start getting problems, so i changed all the double quotes to single quotes and used double quotes for the echo, but still it will not accept it, i think i got to many single quotes now. and i think i need to do something different with "someID"?
echo code:
echo "<img src='img_fjords.jpg' onclick='document.getElementById('someID').style.display='block'' class='w3-hover-opacity'";
You have to escape double quotes if your string is put in double quotes by using backslash \ character. Here is an example:
echo "String with \"double quotes\"";
So in your case it will be:
echo "<img src=\"img_fjords.jpg\" onclick=\"document.getElementById('someID').style.display='block'\" class=\"w3-hover-opacity\"";
Ref: Double quotes within php script echo
You can escape the quotes where needed (using backslashes before the quote signs):
echo "<img src='img_fjords.jpg' onclick='document.getElementById(\"someID\").style.display=\"block\"' class='w3-hover-opacity'";

php - alternative for document.getElementById [duplicate]

This question already has answers here:
PHP quotes inside quotes
(3 answers)
Closed 5 years ago.
hi guys i just want to know if there is an alternative syntax for onclick="document.getElementById('id01').style.display='none'" on php. Beceause its not working even it is inside the echo.tnx
here is my code
First up: document.getElementById is a construct used in JavaScript and has nothing to do with PHP.
The problem you are running into is that you are not escaping the single quotes in your string:
echo '<button onclick="document.getElementById(' <- there is your problem
PHP thinks that the string ends here since you told it so with '.
But the string is in fact not at its end so you need to escape the single quote in order to make it work:
echo '<button onclick="document.getElementById(\'id0\')...';
This goes for all single quotes in your string of course.

using no quotes vs single quotes vs double quotes in $_POST value [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Should I use php quote escapes for single quotes or use double quotes in arrays?
Is it okay to use array[key] in PHP?
what is the difference between these three $_POST values? :
$_POST[data];
$_POST['data'];
$_POST["data"];
The first one, the index is the constant data. Since that is likely undefined, PHP will often just convert it to the string 'data' and log a warning message.
The second two are both identical. The index is the string 'data'.
[Short addendum, since this is a dupe.]
This is considered technically wrong, unless a constant foo had been defined.
print $_POST[data];
Only in double quoted context it is valid (actually required sans curly quotes) to leave out the array keys:
print " use $_POST[data] in double quote context";
Btw, also check the manual (it can also be freely downloaded!) on these topics:
http://php.net/manual/en/language.types.string.php
http://php.net/manual/en/language.types.array.php
You are taking it slightly wrong.
These quotes has nothing to with "POST value".
You can use almost any PHP expression as an array key - a string, a variable, a constnt, a function call.
I your case these keys being regular PHP strings.
And as a string it ought to be quoted - that's all
As for the quotes - there is no difference in this case.
Double quotes accept some special characters to interpret, you can see the list in the manual.
But as there are no special characters in your strings - there is no difference, which quotes to use.

Categories