Php echo " and ' - php

Can someone help me with this code, I know what I'm doing wrong but I don't know how to repair it.
echo "<div onclick='showplayer('".$usersList["username"]."')' id='playerLookupNameResult' style='color:".$color.";'>Some text</div>
I need to use " after onclick= but it is already used after echo.

you can escape " like this \".
echo "<div onclick=\"'showplayer('".$usersList["username"]."')'\" id='playerLookupNameResult' style='color:".$color.";'>Some text</div>

Related

Escape quotes on onclick metho

I have click method generated by php echo. It does not render as it should be. It shows as in the attached image.
my code is
echo "<div class='col-sm-3' onclick='ViewItem('".$item['item_id']."')' style='cursor:pointer'>";
how can I escape the quotes to get the following
<div class='col-sm-3' onclick='ViewItem("1")' style='cursor:pointer'>
We have all been there looking at code too long.
$item['item_id'] = '6';
echo "<div class='col-sm-3' onclick='ViewItem(\"".$item['item_id']."\")' style='cursor:pointer'>";
Output:
<div class='col-sm-3' onclick='ViewItem("6")' style='cursor:pointer'>

Link PHP inside an echo tag

I am having a problem with this code
<?php
echo '<div class="post_note2">
<b>'.$lang['RENEW_SUCCESS'].'</b></div><br /><span class="orange"><b>HOME|VIEW AD</b></span>';
}
}?>
for some reason when the VIEW AD link is clicked it doesn't build it properly and still contains the php code in the link rather than the link to the actual ad page. is it an issue with an echo in an echo ?
I'm sure this isn't quite difficult to solve but I have been trying for far to long on my own and cant get it.
Thanks, any help would be great.
You actually had it right in the first part of your string. You can't have and echo statement inside of another echo statement. Use concatenation throughout your string:
<a href="' . $adurl . '"
You have two extra brackets at the end and php text inside your echo.
<?php
echo '
<div class="post_note2">
<b>'.$lang['RENEW_SUCCESS'].'</b>
</div>
<br />
<span class="orange">
<b>
HOME | VIEW AD
</b>
</span>';
?>
All fixed given that $adurl is defined.
This
<?php echo $adurl; ?>
Should be
' . $adurl . '
i.e.
echo '<div class="post_note2"><b>'.$lang['RENEW_SUCCESS'].'</b></div><br /><span class="orange"><b>HOME|<a href="'.$adurl.'>VIEW AD</a></b></span>';

variables in img src

this is going to an easy one i think but for the life of me i cant get it to work.
i have a variables $id i want to put it in a img src that i have echo (eg. "uploaded/$id.jpg")
i have tried lots of way and looked all over the net and cant get it to work
echo "
<td>
<img src="uploaded'.$id'.jpg">
</td>
";
this is what the echo looks like if anyone can tell me why it is not work would be a big help
It's because you're mixing your double quotes with single quotes and you forgot the . concat operator after $id. Try this:
echo '
<td>
<img src="uploaded/'.$id.'.jpg">
</td>
';
No one stated the obvious:
Separate HTML and PHP, don't echo HTML:
<td>
<img src="uploaded/<?php echo $id; ?>.jpg">
</td>
You are mixing single and double quotes. Try this:
echo "<td>
<img src='uploaded/{$id}.jpg' />
</td>";
You're using double quotes inside a double quoted string.
echo "<td>
<img src='uploaded{$id}.jpg'>
</td>
";
Your mixing of quote styles is the problem. This should work.
or
echo "<td>
<img src=\"uploaded{$id}.jpg\">
</td>
";
if you're worried about valid html... 100 ways to skin a cat.
Try this:
echo "<td>
<img src=\"uploaded/{$id}.jpg\">
</td>";
EDIT: I think you were missing a / in the image path. Is "uploaded" a directory?
You were mixing single and double quotes. Double quotes allow you to embed variable in them like this... "Hello, $name" but if you tried "name$firstBlue", PHP would assume you were trying to insert the value of $firstBlue. You'd have to use "name{$first}Blue" for that to work. Always placing variables in {curly braces} is a good habit, because it prevents silly mistakes.
why it is not working.
echo "//double quotes begins here
<td>
<img src="/*it will end here later portion will not be printed*/ uploaded'.$id'.jpg">
</td>
";
you can use
echo '<td>
<img src="uploaded/'.$id.'.jpg">
</td>
';

simple Echo Error

i try to make a while{ part in php, to read out a mysql db. Works perfectly, only with the output i have problems:
echo "<div id=\"content\"><ul class=\"pageitem\"><li class=\"store\"><span class=\"image\" style=\"background-image: url('<?php echo \". $zeile['image'] . \"; ?>')\"></span><span class=\"name\"><?php echo \". $zeile['title'] . \"; ?></span><span class=\"arrow\"></span></li></ul>";
Error is:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in D:\xampplite\htdocs\harti\products.php on line 40
Sry i'm really new with php, i think this is a simple mistake but cannot find it.
thx for help
Don't use <?php again...
echo "<div id=\"content\"><ul class=\"pageitem\"><li class=\"store\"><span class=\"image\" style=\"background-image: url('". $zeile['image'] . "')\"></span><span class=\"name\">". $zeile['title'] . "</span><span class=\"arrow\"></span></li></ul></div>";
Also, you forgot to close the DIV.
Instead, you could do something like this (outside <?php and ?>):
<div id="content">
<ul class="pageitem">
<li class="store">
<a href="index.html">
<span class="image" style="background-image: url('<?php echo $zeile['image']; ?>')"></span>
<span class="name"><?php echo $zeile['title']; ?></span>
<span class="arrow"></span>
</a>
</li>
</ul>
</div>
Read up on how strings work in PHP:
http://php.net/manual/en/language.types.string.php
If you use double quotes you can do this:
echo "<b>{$myarray[$index]}</b>";
Or you can do this:
echo 'blah';
Note how I used single quotes to avoid having to escape the double quotes. However, with a single quote, I cannot embed the variable within the quote.
Try this
echo "<div id='content'><ul class='pageitem'><li class='store'><a href='index.html'><span class='image' style='background-image: url(" . $zeile['image'] . ")'></span><span class='name'>" . $zeile['title'] . "</span><span class='arrow'></span></a></li></ul></div>";
Do not use
try this:
echo "<div id=\'content\'><ul class=\'pageitem\'><li class=\'store\'><a href=\'index.html\'><span class=\'image\' style=\'background-image: url(".$zeile['image'].")></span><span class=\'name\'>".$zeile['title']."</span><span class=arrow></span></a></li></ul></div>";
Check Single quotes also.
And even I agree with "Parkyprg", use html content outside php.
you may replace each " with "+"\""+".
It works in all scripting languages.

echo line returns in html source code

How do you implement line returns when the html code is between simple quotes. The only way I found is to concatenate the line return between double quotes:
echo '<div>'."\n"
.'<h3>stuff</h3>'."\n"
.'</div>'."\n";
Which lucks ugly to me.
Edit: The code between the simple quotes is quite long and with many attributes otherwise I would just use double quotes.
echo "<div>\n" .
"<h3>stuff</h3>\n" .
"</div>\n";
or
echo "<div>
<h3>stuff</h3>
</div>\n";
or
echo <<< HTML
<div>
<h3>stuff</h3>
</div>
HTML;
But this is completely subjective.
You can do
echo "<div>\n<h3>stuff</h3>\n</div>\n";
or
echo '<div>
<h3>stuff</h3>
</div>
';
Concatenation: echo '<div>' . "n";
Concatenation with constant: echo '<div>' . PHP_EOL;
Hard line feeds:
echo '<div>
';
End PHP mode: echo '<div>'; ?>
Double quotes: echo "<div>\n";
Heredoc:
echo <<<EOM
<div>
EOM;
Single quotes inhibit backslash sequences. But newlines are pretty much optional outside of CDATA sections, so feel free to omit them.
echo <<<EOM
<div>
<h3 class="blabla" style='booboo' >stuff<h3>
</div>
EOM;
Using the <<< quotation, you don't need to escape any characters(they are simply outputted exactly how they are typed.

Categories