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
<?
}
?>
Related
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'>
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.
I'm trying to call an HTML/PHP content that it's inside my database using:
<?php echo $row_content['conteudo']; ?>
When the row is called the HTML appears correctly but the PHP doesn't.
I belieave it's cause of the echo inside the main echo.
<?php echo "
<h3>Hello</h3>
<?php do { ?>
<div class=\"indios\">
<a href=\"indio.php?id=<?php echo $row_indiosct['id']; ?>\">
<img src=\"galeria/indios/<?php echo $row_indiosct['foto']; ?>\" alt=\"<?php echo $row_indiosct['nome']; ?>\" />
<br /><?php echo $row_indiosct['nome']; ?></a></div>
<?php } while ($row_indiosct = mysql_fetch_assoc($indiosct)); ?> "
?>
The line one of this code is the same echo as the first code field, it's not repeating, it's there just for help and to understand where is the problem.
I already fixed some quotation marks but it gives an error in the line of the 1st echo.
That is some of the ugliest code I have ever seen...
<?php
echo '
<h3>Hello</h3>';
while ($row_indiosct = mysql_fetch_assoc($indiosct))
{
echo '
<div class="indios">
<a href="indio.php?id='.$row_indiosct['id'].'">
<img src="galeria/indios/'. $row_indiosct['foto'].'" alt="'.$row_indiosct['nome'].'" />
<br />'.$row_indiosct['nome'].'</a>
</div>';
}
?>
You could also use the HEREDOC syntax.
Don't do this. Multi-line echoes, especially when you've got embedded quotes, quickly become a pain. Use a HEREDOC instead.
<?php
echo <<<EOL
<h3>Hello</h3>
...
<div class"indios">
...
EOL;
and yes, the PHP inside your echo will NOT execute. PHP is not a "recursively executable" language. If you're outputting a string, any php code embedded in that string is not executed - it'll be treated as part of the output, e.g.
echo "<?php echo 'foo' ?>"
is NOT going to output just foo. You'll actually get as output
<?php echo 'foo' ?>
You have misunderstood how PHP works. PHP is processed by the server. When it encounters your script, it sees the following:
<?php echo "some long piece of text that you have told PHP not to look at" ?>
What is the reasoning behind trying to nest PHP calls inside strings?
evaluate code php in string using the function eval(): this post Execute PHP code in a string
<?php
$motto = 'Hello';
$str = '<h1>Welcome</h1><?php echo $motto?><br/>';
eval("?> $str <?php ");
http://codepad.org/ao2PPHN7
also if your need the code buffer output in a string also you can using the ob_start() method:
<?php ob_start(); ?>
<h3>Hello</h3>;
<?php
while ($row_indiosct = mysql_fetch_assoc($indiosct)){ ?>
<div class="indios">
<a href="indio.php?id='<?php echo $row_indiosct['id']'">
<img src="galeria/indios/'<?php echo $row_indiosct['foto'].'" alt="'.$row_indiosct['nome'].'" />
<br />'.$row_indiosct['nome'].'</a>
</div>';
<?php } ?>
<?php
if(get_field('member_only_content')){
echo "do something";
}else{
echo "do something else";
}
?>
How do I insert HTML code where it says "do something"? When removing echo "do something"; and pasting the HTML code the Wordpress site crashes.
Close and open the PHP blocks, like this:
if(get_field('member_only_content')){
?>
<html></html>
<?php
} else{
?>
<html></html>
<?php
}
You can also use PHP's alternative syntax for this:
if(get_field('member_only_content')): ?>
<html></html>
<?php else: ?>
<html></html>
<?php endif;
Like this!
<?php
if(get_field('member_only_content')) : ?>
<div>Html stuff</div>
<?php else : ?>
<div>More Html stuff</div>
<?php endif;
Put HTML in place of the string.
<?php
if(get_field('member_only_content')){
echo "<your>HTML here</your>";
}else{
echo "<other>HTML</other>";
}
?>
You can also break out of the PHP tags
<?php
if(get_field('member_only_content')){
?>
<your>HTML here</your>
<?
}else{
?>
<other>HTML</other>
<?
}
?>
That would be because you placed the HTML code within php tags (<?php ?>). Text inside this tag is interpreted as a PHP instruction, thus it won't render HTML. There are 2 general ways render the HTML in a PHP file:
Echo the HTML
<?php
if (get_field ('member_only_content')) {
echo "<span>Put HTML here</span>";
}
else {
echo "<span>Put HTML here</span>";
}
?>
Place the HTML Outside PHP Tags
<?php if (get_field ('member_only_content')): ?>
<span>Put HTML here</span>
<?php else: ?>
<span>Put HTML here</span>
<?php endif;?>
Or you can use the <<< statement:
echo <<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon. no extra whitespace!
END;
Straight from http://php.net/manual/en/function.echo.php
<?php
if(get_field('member_only_content')){
echo "<div style='background-color: #888888'>This is a simple string between div's, make sure you've to be careful while inserting too many single and double quotes in this string as well as inline styles</div>";
}else{
echo "do something else";
}
?>
Be careful while inserting quotes in your string.
Sample code
echo("<h1>This is a sample</h1>");
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.