echo line returns in html source code - php

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.

Related

php variable inside echo 'html code'

I have php file index.php
In this file to use html code I am doing:
index.php
echo '
<div>
<a class="fragment" href="">
<div>';
In href I want to put value of some php variable i.e. $url How could be done?
is this correct way?
<a class="fragment" href="<?php echo $url; ?>">
You concatenate the string by ending it and starting it again:
echo '
<div>
<a class="fragment" href="' . $url . '">
<div>';
Though I personally prefer to stop the PHP tags and start them again (if I have a lot of HTML) as my IDE won't syntax highlight the HTML as it's a string:
?>
<div>
<a class="fragment" href="<?php echo $url; ?>">link</a>
</div>
<?php
Since you are printing several lines of HTML, I would suggest using a heredoc as such:
echo <<<HTML
<div>
<a class="fragment" href="$url">
<div>
HTML;
HTML can be anything as long as you use the same tag both in the beginning and the end. The end tag must however be on its own line without any spaces or tabs. With that said, specifically HTML also has the benefit that some editors (e.g. VIM) recognise it and apply HTML syntax colouring on the text instead of merely coluring it like a regular string.
If you want to use arrays or similar you can escape the variable with {} as such:
echo <<<HTML
<div>{$someArray[1]}</div>
HTML;
if you are echoing php directly into html i like to do this
<div><?=$variable?></div>
much shorter than writing the whole thing out (php echo blah blah)
if you are writing html in php directly then there several options
$var = '<div>'.$variable.'</div>'; // concatenate the string
$var = "<div>$variable</div>"; // let php parse it for you. requires double quotes
$var = "<div>{$variable}</div>"; // separate variable with curly braces, also requires double quotes
Do it like
<?php
$url='http://www.stackoverflow.com';
echo "<div><a class='fragment' href='$url' /></div>";
If you want to maintain the echo statement you can do either
echo '<a class="fragment" href="'.$url.'"><div>';
or
echo "<a class=\"fragment\" href=\"$url\">";
The first is better for performances and IMHO is more readable as well.
If you want to input/output large blocks of HTML with embedded variables you can simplify the process by using Heredocs:
echo <<<_EOI_
<div>
<a class="fragment" href="$url">
<div>
_EOI_;
You don't have to worry about escaping quotes, constant concatenation, or that ugly dropping in and out of <?php echo $var; ?> that people do.

Php echo " and '

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>

Syntax for putting an onClick action in an echo statement

What would be the correct syntax for the onClick attribute in the following:
<?php
if($_resp['user']) {
echo '<div class="logo">Link Text';
}
?>
Since you are nesting both double and single quotes, you will need to escape the single quotes in the onClick with backslashes.
echo '<div class="logo">Link Text';
Consider using a HEREDOC
if ($_resp['user']) {
echo <<<EOL
<div class="logo">Link Text
EOL;
}
or dropping out of PHP mode:
if ($_resp['user']) { ?>
<div class="logo">Link Text
<?php }
Both get the job done, and eliminate any need to escape quotes inside the html/javascript you're generating
You can mix HTML and PHP, so you might want to simply move it out of PHP:
<?php
if($_resp['user']) {
?>
<div class="logo">Link Text
<?
}
?>

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.

Categories