echo an <img> tag - php

I'm going crazy trying to echo an <img> tag.
I want to add this simple string "/uploads/" before $photo->name.
Here is my echo: echo '<img src="'.$photo->name.'"/>';
Thanks for any help.

Not sure if I completely understand, but try this:
echo '<img src="/uploads/'.$photo->name.'"/>';

Here you go:
echo '<img src="/uploads/'.$photo->name.'"/>';
Or:
echo '<img src="' . '/uploads/' . $photo->name.'"/>';
Or:
echo '<img src="' . "/uploads/" . $photo->name.'"/>';

echo '<img src="/uploads/'.$photo->name.'"/>';

How about
echo '<img src="/uploads/'.$photo->name.'"/>';
... unless I'm missing something?

two ways to use echo tag
1st : one is double quotes which use for out put of strings
echo "your name is".$name //here name is variable
2nd : one is single quotes in which you can use all html tags here is the example
<?php
$name="Adil";
echo $name;
for($i=0;$i<44;$i++)
{
echo($i.'<br>') ;
if($i==10)
{
echo ' <table border=3> <tr><td><h1>its image </h1><img src="examp.png" alt="loading" /><td><td>hello</td><tr></table>';
}
}
?>
here i am using multiple tags in echo using
single quotes
if put double quotes then you will see following error
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\xampp\htdocs\Untitled2d.php on line 21
here is the output

Related

echo background image with a value

I echo out an image like that:
$newString = $thumbPre.'profilemain'.$thumbPost;
echo "<img src='http://render-api-us.worldofwarcraft.com/static-render/us/" . $newString. "' alt='error'>";
Now i want the image as a background-image, i tried it like that, but it doesn´t work:
echo '<div style="background-image:url('http://render-api-us.worldofwarcraft.com/static-render/us/" . $newString. "' alt='error');"></div>';
you need to use backslashes for nested apostrophes
echo '<div style="background-image:url(\'http://render-api-us.worldofwarcraft.com/static-render/us/' . $newString.'\' alt=\'error\');></div>';
Firstly, remove alt='error' because background-image does not have an alt parameter, img does (you probably thought you could use that from your original code). In trying to use that, your background will not show up.
And your background won't show unless you have content inside that div. I've added Content as an example.
echo '<div style="background-image:url(\'http://render-api-us.worldofwarcraft.com/static-render/us/' . $newString.'\');">Content</div>';
You either have to escape the encapsulating quotes, or remove them altogether.
echo '<div style="background-image:url(http://render-api-us.worldofwarcraft.com/static-render/us/' . $newString.');">Content</div>';
Error reporting would have also thrown you a parse error such as:
Parse error: syntax error, unexpected '' alt='' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';'
http://php.net/manual/en/function.error-reporting.php
This is going to look super confusing but you need to 1, escape the quotes and 2, concatenate your path within that,
let me give you an example. I misread a bit of the question but this will serve you well moving forward especially for cleanliness sake
here is an example:
$imagePath ='PATH TO IMAGE HERE';
echo '<div style="background-image:url(\'' .$imagePath. '\')" >STUFF HERE </div>';

php anchor tag with image in between

hello i keep getting an error message when i try to place a image file inbetween 2 anchor tags. i am using echo for the obvous reason i am using php but i get this message when i try to refresh the page:
*Parse error: syntax error, unexpected '<' in E:\Game Downloads\Applications\Xampp\htdocs\WEBSITE DESIGN(PHP)\signed_in.php on line 189*
below is what i have got so far but ive been messing around with the way the speech marks are used and the quotation marks
echo"<div class='add_to_cart'>";
echo"<a href='#'>""<img src='Images/ADD-TO-CART-copy-4.png' />" "</a>";
echo"</div>";
Make it simple as below -
echo '<img src="Images/ADD-TO-CART-copy-4.png" />';
Try this:
echo "<div class='add_to_cart'><a href='#'><img src='Images/ADD-TO-CART-copy-4.png' /></a></div>";
Try this:
print '<div class="add_to_cart">';
print '<img src="Images/ADD-TO-CART-copy-4.png" />';
print '</div>;
or similarily:
print '<div class="add_to_cart"><img src="Images/ADD-TO-CART-copy-4.png" /></div>';

Echoing PHP within HTML

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>

Apparent PHP Syntax Error

Basically, I've been trying to make a simple Wordpress widget that displays a QR code with the URL of the current page. I'm using a modififed version of the simple text widget that parses PHP too.
function the_qrcode($permalink = '', $title = '') {
if($permalink && $title == '') {
$permalink = 'http://eternityofgamers.com/forums';
$title = 'Forums';
}
echo '<img src="http://api.qrserver.com/v1/create-qr-code/?data=' .$permalink. '" alt="QR: ' .$title. '"/>;
}
Can someone tell me what's wrong with this? I get a 500 error when I add it to functions.php.
Look at StackOverflow's syntax highlighting. You're missing a closing single quote ' on your string at the end of the function's last line:
echo '<img ...' .$title. '"/>;
^
Close the last single quote on the echo line to be:
echo '<img src="http://api.qrserver.com/v1/create-qr-code/?data=' .$permalink. '" alt="QR: ' .$title. '"/>';

My php variables are not being printed out.

<?php if ( is_user_logged_in() ) {
echo '<img id="visit-the-forums" src="<?php bloginfo('template_url') ?>/images/visit-the-forums.png" alt="Check out the Forums!" />'
} else {
echo '<img id="join-the-forums" src="<?php bloginfo('template_url') ?>/images/join-the-forums.png" alt="Join the Forums!" />'
}
?>
I think there is something wrong w/ the way I set up the "php bloginfo" code inside but I'm not sure how to fix it.
The code below should work for you. You had to make use of string concatenation:
<?php if ( is_user_logged_in() ) {
echo '<img id="visit-the-forums" src="' . bloginfo('template_url'). '/images/visit-the-forums.png" alt="Check out the Forums!" />'
} else {
echo '<img id="join-the-forums" src="' . bloginfo('template_url') . '/images/join-the-forums.png" alt="Join the Forums!" />'
}
?>
You have 2 problems:
Most likely this code is not
executing because you're echoing a
string that's delimited with single
quotes and inside it you've put
unescaped single quotes. (You can tell this is the case because even on this page, the syntax colouring is messed up :)
Even if you had escaped the single
quotes (e.g. <?php
bloginfo(\'template_url\') ?>) this
would not work because you're using
PHP to echo PHP code, which will then
be passed to the browser, instead of
being executed by the PHP engine.
What you need to do is to add the result of bloginfo() (or get_bloginfo(), see edit below) to the string you're outputting:
echo '<img id="visit-the-forums" src="'. bloginfo('template_url') . '/images/visit-the-forums.png" alt="Check out the Forums!" />'
(note the correct usage of single-quotes as delimiters, and the correct syntax highlighting on this page: strings are reddish, code is black)
EDIT: if bloginfo here is the WordPress function, you will want to replace it in my code above with get_bloginfo which actually returns the result rather than printing it, but your original question wasn't clear about what bloginfo is/does.

Categories