I'm having trouble using the echo function in php. I don't fully understand when double quotes, single quotes, and escaping quotes are necessary. I've tried several variations and I can't seem to get it to show up. I know how to echo basic html, but I've not used classes before, I think that might be the dealbreaker here.
Edit: flagged for being a duplicate, I'm new here, not really sure what to do. But it's not duplicate post, the other post uses elements that I'm not familiar with using, this was just straight html. Thank you.
<?php echo "<div id=\"resume-links\" style='display:flex;float:right;''>
<a class='button button--type-action--size-medium' href='/myaccount' style='margin-right:10px;''>Dashboard →</a>
<a class='button button--type-action--size-medium' href='#popmake-1301'>Get Featured →</a>
</div>
<div id=\"resume-link\" style='clear:both;float:right;padding-top:10px;font-style:italic;'><p><a href='/article-submissions'>Learn More</a></p></div>";?>
You can use
<<<START
htmlHere
START;
And after margin-left you hav 2 ' but must 1
you can wrap single quotes inside double quotes
echo " <div id='resume-id'>qwerty</div> ";
you can wrap double quotes inside single quotes
echo '<div id ="resume-id">qwerty</div>';
you can use escape character when your wrapper quotes are same as inner quotes
echo "<div id =\"resume-id\">qwerty</div>";
or
echo '<div id ="resume-id">qwerty\'s</div>';
output : qwerty's
Use Heredoc syntax.
Example :
$str = <<<EOD
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
</head>
EOD;
Documentation
Related
Probably a newbie thing but I can't get the meta name="description" row to work. You should close and open PHP-code if its nested, right?
Thanks!
/Robert
<?php
$blogname = get_bloginfo('name');
if ($blogname == 'Wobbling Willy') {
echo '<meta name="description" content="?><?php bloginfo('description'); ?><?php" />'."\n";
echo '<meta name="keywords" content=”keyword1, keyword2, keyword3” />'."\n";
}
?>
I like this syntax myself ( when working in mixed HTML/PHP )
<?php
$blogname = get_bloginfo('name');
if ($blogname == 'Wobbling Willy'): //-note- colon not semi-colon
?>
<meta name="description" content="<?= bloginfo('description'); ?>" />
<meta name="keywords" content=”keyword1, keyword2, keyword3” />
<?php endif; ?>
OR
<?php
$blogname = get_bloginfo('name');
if ($blogname == 'Wobbling Willy'){ //open bracket
?>
<meta name="description" content="<?= bloginfo('description'); ?>" />
<meta name="keywords" content=”keyword1, keyword2, keyword3” />
<?php } // close bracket?>
It's shorter and you don't have to worry about quoting. Or even use a heredoc
but that involves setting bloginfo('description') to a variable and a few other quirks...
<?php
$blogname = get_bloginfo('name');
if ($blogname == 'Wobbling Willy'){ //open bracket
$description = bloginfo('description');
echo <<<HTML
<meta name="description" content="$description" />
<meta name="keywords" content=”keyword1, keyword2, keyword3” />
HTML; //nothing can go here no space ( before or after ) and not even this comment, nothing but HTML; litterally
}
?>
HEREDOC is my preferred way, but I never mix HTML and PHP in the same file. Because I use a template system.
Try this:
<?php
$blogname = get_bloginfo('name');
if ($blogname == 'Wobbling Willy') {
echo '<meta name="description" content="'.bloginfo('description').'"/>\n';
echo '<meta name="keywords" content="keyword1, keyword2, keyword3"/>\n';
}
?>
In reply to the OP's question about nested PHP tags, the nesting of PHP opening and closing tags only applies to doing so within HTML code, as follows:
<div id="<?php echo $id; ?>">
Note: this style of mixing HTML and PHP together was far more popular years ago; nowadays this is not considered the best style. Instead, the emphasis is on a separation of concerns, as reflected in the model-view-controller design pattern. And, nesting PHP code within PHP is not the thing to do and will likely produce a parse error.
Of the various ways to fix the code, you may wish to view my live demo which makes use of heredoc syntax.
What is interesting about the OP's snippet is that it actually does not have any PHP tags nested within PHP code. In fact, if you remove the superfluous tags, allowing for only one open tag, the code just needs minor tweaking to work, as follows:
// these function implementations are just for demo purposes:
function get_bloginfo($n) {
return ("Wobbling Willy");
}
function bloginfo($d){
return("some lovely description ...");
}
$blogname = get_bloginfo('name');
if ($blogname == 'Wobbling Willy') {
echo '<meta name="description" content="';
echo bloginfo('description'),'"/>',"\n";
echo '<meta name="keywords" content="keyword1, keyword2, keyword3" />'."\n";
}
See demo
The return value of bloginfo() will not appear unless provided as a parameter to a function or construct, such as echo, which will output it. Splitting up the display of the "description" meta tag while awkward will work provided that one uses the single quotes as needed. Lastly, curly quotes can be problematic and in this case I replaced those enveloping the second meta tag's content with straight ones to enclose the keywords.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have created many sites that requires php, html and css. They all work fine but i'm now thinking if all that time i do it in a wrong way.
Usually, first i build the site template and styling and then i code the php.
Then i just include the php functions etc in the HTML.
For example take a look of a table that lists some posts (index.php):
<div class="posts">
<h1 class="content-title">New Posts</h1>
<?php
$rows = list_posts();
while ($row=mysql_fetch_array($rows)) {
echo "
<div>
<img class='post-avatar' src='img/icon_newsletter.jpg'>
<h2 class='post-title'>".$row['post_title']."</h2>
<p class='post-meta'>
Από <a href='#'>".$row['post_user']."</a> | ".$row['post_date']." | <a class='post-views' href='viewpost.php?id=".$row['post_id']."#comments'>Comments: ".total_comments($row['post_id'])."</a>
</p>
<p>
".substr($row['post_body'], 0, 180)."...
</p>
<img class='post-icon-read' src='img/read.png' /> <a href='viewpost.php?id=".$row['post_id']."'>Συνέχεια άρθρου..</a>
</div>
";
}
?>
</div>
There is room for improvement.
Keep the HTML within the PHP. It iss not personal opinion, there are technical reasons.
When you do this:
html
<?php PHP ?>
html
<?php ?>
html
<?php PHP ?>
html
There is overhead within PHP when you switch back and forth between HTML mode and PHP Mode. And it makes you look like a Word Press Hack.
A few things:
- Heredoc syntax
- Numeric Arrays
- escape quotes
Start with the basic template:
echo <<<EOT
// Top of page
EOT;
while ($row=mysql_fetch_array($rows), MYSQL_NUM) {
}
echo <<<EOT
// bottom of page
EOT;
The reason to use MYSQL_NUM is so the column values can be use without concatenation.
Instead of
<a href='#'>".$row['post_user']."</a> | ".$row['post_date']." | <a class='post-views' href='viewpost.php?id=".$row['post_id']."#comments'>Comments: ".total_comments($row['post_id'])."</a>
you can do this:
$comment = total_comments($row[2]) ;
echo "$row[0]$row[1]<a class=\"post-views\" href=\"viewpost.php?id=$row[2]#comments\">Comments: $comment</a>";
The backslash will escape the double quotes within double quotes. Quote marks do not need to be escaped within Heredoc.
Single dimension Numeric arrays can be used within double quotes and within Heredoc.
Top of page:
echo <<<EOT
<!DOCTYPE html>
<html lang="en"><head><title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Edit Rotation Diet</title>
<style type="text/css">
</style></head>
<body><div id="page">
EOT;
Bottom of page:
echo <<<EOT
<script type="text/javascript">//<![CDATA[
//]]>
</script></body></html>
EOT;
Let's say the page content is a table
<body><div id="page"><table>
EOT;
while ($row=mysql_fetch_array($rows), MYSQL_NUM) {
echo <<<EOT
<tr>
<td>$row[0]</td>
<td>$row[1]</td>
<td>$row[2]</td>
<td>$row[3]</td>
</tr>
EOT;
}
echo <<<EOT
</table>
<script type="text/javascript">//<![CDATA[
Indentation of HTML along with PHP is wrong. You should think about how the HTML is going to look when someone views the source. View the source on most any Word Press page.. Looks like crap.
The blank line between the echo and first html gives you a line feed.
When you echo HTML like you did it ends up looking real bad when you view the source, like Word Press.
Personally, I do not like a four space indent. Before you know it your code is hidden beyond the right border. I use two spaces.
I think this is ugly and a waste of vertical space:
if($x = 1)
{
echo '<p>one</p>';
}
else
}
echo '<p>two</p>'
}
I think this is plenty clear. Without having excessive vertical scrolling/
if($x = 1){
echo '<p>one</p>';
}
else{
echo '<p>two</p>
}
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.
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
<?
}
?>
My PHP is not PHPing, so made simple test... must be missing something obvious.
<html>
<head>
</head>
<body>
<?php echo '<script language=\"javascript\">confirm("Do you see this?")</script>;'; ?>
</body>
</html>
In code body, I get: confirm("Do you see this?");'; ?>
When I "View Source", I see:
<html>
<head>
</head>
<body>
<?php echo '<script language=\"javascript\">confirm("Do you see this?")</script>;'; ?>
</body>
</html>
what extension has your file? is a webserver running? how are you calling your php script?
make sure it has a .php extension, the webserver is running, the file resides under the webroot directory and you call it via http://localhosti/path/to/file.php
also make sure you don't escape quotation marks when not needed, echo '<script type="text/javascript">…</script>'; should do the job
You should remove the backslashes from the \"javascript\".
<?php echo '<script language="javascript">confirm("Do you see this?")</script>;'; ?>
In PHP you can put strings in single ' or double " quotes. This is quite hard to explain (and/or understand) in a few lines, so here's a few valid ways to write down a string containing quotes:
echo 'This has "double quotes"...';
echo 'This has \'single quotes\'...';
echo "This has \"double quotes\"...";
echo "This has 'single quotes'...";
There are many more subtleties to this, but this should get you started.
Take out the \ from the double quotes and the extra semi colon
<html>
<head>
</head>
<body>
<?php echo '<script language="javascript">confirm("Do you see this?")</script>'; ?>
</body>
</html>