This question already has answers here:
Print string with a php variable in it
(4 answers)
Closed 11 months ago.
How would I write this so I'm only using one echo?
echo "<div class='post'>";
echo $row['title'];
echo "</div>";
I'm using an array to echo out a table's values but it would be a lot easier on the eyes if I could combine these into one echo statement. However, when I try to the page goes blank.
echo "<div class=\"post\">{$row['title']}</div>";
Other ways of doing the same thing:
echo '<div class="post">'.$row['title'].'</div>';
echo sprintf('<div class="post">%s</div>', $row['title']);
<div class="post"><?php echo $row['title'] ?></div>
you forgot to close the div
> echo "<div class='post'"; should be
> echo "<div class='post'>";
Enclose the array reference in brackets:
echo "<div class='post'{$row['title']}</div>";
<?php
echo "<div class='post'".$row['title']."</div>";
?>
echo '<div class="post">' . $row['title'] . '</div>';
The . operator concatenates strings. Also, I believe HTML wants you to use double quotes for class="post" (although perhaps it doesn't care?).
Related
Please help me to understand syntax of concat following
i want following code inside echo to understand concate
1--> <div class="alert echo $_REQUEST['error_id'];?>"\>
I try following code but getting syntax error that there is two echo..
2--> echo "<div class=\"alert" . echo $_REQUEST['error_id']; . "\">";
where mistake is that i cant get it... and i want two answer both using single quote and double quote
EDIT
Thank #Rizier123 now it working but css is not working as i have apply in calss="alert"
while using following code its working fine
<div class="alert echo $_REQUEST['error_id'];?>"\>
But after applying your code its not working..only text appear but background color is not comming nad boarder is also dissappear as below
class name is alert shows the status of login..
EDIT
Thanks again i just forget to put space after class name..
You don't need to write echo and ; again!
So this should work:
//echo with double quotes
echo "<div class=\"alert" . $_REQUEST['error_id'] . "\">";
//echo with single quotes
echo '<div class="alert' . $_REQUEST['error_id'] . '">';
Maybe you need to add a space if your class name includes spaces!(..."alert "...)
try this
echo '<div class=\"alert"' . $_REQUEST['error_id'] . '">';
You can do either 2 ways
1. PHP in HTML
<div class="alert<?php echo $_REQUEST['error_id'];?>">
2. HTML in PHP
echo "<div class='alert". $_REQUEST['error_id'] ."'>";
Or you can also do like
echo "<div class=\"alert". $_REQUEST['error_id'] ."\">";
This question already has answers here:
Print string with a php variable in it
(4 answers)
Closed 11 months ago.
If I execute this I am getting output properly:
echo "'.$elink.'";
but when I want to display my output in a table column format I am not able to insert:
echo "<td width='200'>" '.$elink.' "</td>";
or
echo "<td width='200'>" "'.$elink.'" "</td>";
or
echo "<td width='200'>" ''.$elink.' "</td>";
Please correct the syntax errors.
echo '<td width="200">' . ''.$elink.'</td>';
Looks like you have mismatched quotes. But I would use sprintf.
echo sprintf("<td width='200'><a href='%1$s'>%1$s</a></td>", $elink);
First off you quotes are messed up. They should look like this:
echo "<td width='200'> <a href='".$elink."'>".$elink."</a></td>";
Correct your string format
echo '<td width="200">' . $elink . '</td>';
I suggest you to use the attribute style (style="width:200px;") instead the width attribute.
Remember to Url Encode the parameters contained in the href attribute.
I am currently trying to create a shopping cart for my website and I have images of products stored in a database and I want to include them within <img src> . By putting $get_row[imagesrc] within the src. I need to know the correct way to add it to the below code as I dont fully understand the ' and . tags
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/>'.$get_row['imagesrc'].
'<br/>£'.number_format($get_row['price'],2).'Add</p>';
This should achieve what you're looking for:
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/><img src="'.$get_row['imagesrc'].'" /><br/>£'.number_format($get_row['price'],2).'Add</p>';
The ' character defines a string literal when it is wrapped around a series of characters.
The . character is used for concatenating strings for output or storage.
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/><img src="'.$get_row['imagesrc'].'"><br/>£'.number_format($get_row['price'],2).'Add</p>';
. concatenates two strings, and ' is wrapped around a string.
so
echo 'Hello '.'World'; // Shows Hello World
I'd split yours up to make it easier to read:
echo '<p>';
echo $get_row['name'].'<br/>';
echo $get_row['description'].'<br/>';
echo '<img src="'.$get_row['imagesrc'].'" /><br/>';
echo '£'.number_format($get_row['price'],2);
echo 'Add';
echo '</p>';
But it all looks OK.
echo '<p>'.$get_row['name'].'<br/>
<img src="'.$get_row['imagesrc'].'" alt="'.$get_row['name'].'"><br/>
<br/>£'.number_format($get_row['price'],2).'
Add</p>';`
echo '<img src="'.$get_row['imagesrc'].'">';
Try that.
A specific answer has been given:
echo '<img src="'.$get_row['imagesrc'].'">';
Nonetheless, it's worth adding that you should:
You should escape output - with htmlspecialchars() or otherwise.
echo '<img src="' . htmlspecialchars($get_row['imagesrc']) . '">';
Read the documentation on PHP Strings.
Check out this way of including PHP in your HTML. It's much easier to read and maintain. The last line in the paragraph is your image tag.
<p>
<?php echo $get_row['name']; ?><br/>
<?php echo $get_row['description']; ?><br/>
<?php echo $get_row['imagesrc']; ?><br/>
£<?php echo number_format($get_row['price'],2); ?>
Add
<img src="<?php echo $get_row['imagesrc']; ?>" />
</p>
How to wrap a div tag inside other tags and variable using php's echo function?
This php code works:
echo "<strong> $matches[1] </strong>" . "<p> $matches[2] </p>";
But I need the above code to wrap inside a div. I tried the following code below but its not working.
echo "<div>";
echo "<strong> $matches[1] </strong>" . "<p> $matches[2] </p>";
echo "</div>";
You need curly brackets if you want to output an array inside a string.
echo "<div>";
echo "<strong>{$matches[1]}</strong>"."<p>{$matches[2]}</p>";
echo "</div>";
echo "<p id='xlday'>'$_POST['day']'</p>";
Hi can anyone help me solve this problem, should be a simple one but i'm struggling for some reason!
Thanks
echo "<p id='xlday'>".$_POST['day']."</p>";
echo "<p id='xlday'>".$_POST['day']."</p>";
or
echo "<p id='xlday'>{$_POST['day']}</p>";
or
echo "<p id='xlday'>${_POST['day']}</p>";
or
echo "<p id='xlday'>$_POST[day]</p>";
I'll also add
echo "<p id='xlday'>", $_POST['day'], "</p>";
with the mention that it does not actually concatenate the strings, but rather outputs them one at a time.
Do like this:
echo "<p id='xlday'>".$_POST['day']."</p>";
You can also do like:
echo "{$_REQUEST['day']}";
However we prefer output buffering.
http://www.suite101.com/content/output-buffer-in-php-a26768