What' wrong with this syntax? - php

I am sure this is simple, but I don't get why...
Why is this wrong?
echo "<img src='".bloginfo('template_directory')."/systemdata/next.png' border=0 id='NavImage'>";
If I do it like this, it works:
echo "<img src='";
echo bloginfo('template_directory');
echo "/systemdata/next.png' border=0 id='NavImage'>";
What do I do not get? I want to write nice code and the second example is not very elegant I think.
Thanks!

You are using the wrong function, bloginfo() already outputs / echoes so you cannot use it when you want to concatenate strings (nor should you echo it...).
Instead you could use get_bloginfo() as that returns a string:
echo "<img src='".get_bloginfo('template_directory')."/systemdata/next.png' border=0 id='NavImage'>";

You can try this as far as code elegant factor is concerned:
$blogingo=bloginfo('template_directory');
echo "<img src=$blogInfo/systemdata/next.png' border=0 id='NavImage'>";

Related

Included php tag in html attribute

How do I include a php tag in an HTML attribute? I still find it quite tricky.
This is the HTML attribute:
src="https://customer.site.com/?language=en_US&portal=Default"
The thing is, ?language should accept a dynamic value. That value is stored in $lingos[$wmpl_langcode].
So I've been trying many variations and I'm still stuck.
I've got this now but it doesn't seem right.
src=<?php echo "https://customer.site.com/?language=" . $lingos[$wmpl_langcode] . "&portal=Default" ?>"
I don't want to waste any more time on it. Any tips would be great.
you was just missing one double quote and the ; at the end of the echo to end the instruction:
src="https://customer.site.com/?language=<?php echo $lingos[$wmpl_langcode]; ?>&portal=Default"
You are now missing quotes for the attribute.
This however would be perfectly fine:
src="<?php echo "https://customer.site.com/?language=" . $lingos[$wmpl_langcode] . "&portal=Default" ?>"
If you like it a bit more clean without confusing quotes:
<img src="https://customer.site.com/?language=<?php echo $lingos[$wmpl_langcode] ?>&portal=Default"/>
You should also think about urlencode():
<img src="https://customer.site.com/?language=<?php echo urlencode($lingos[$wmpl_langcode]) ?>&portal=Default"/>
To make it "more clean", you should use a templating engine.
When you directly use echo to print the dynamic link, the quotes are not present there.
Try this:
src="https://customer.site.com/?language=<?=$lingos[$wmpl_langcode] ?>&portal=Default"
OR
src="<?php echo "https://customer.site.com/?language=" . $lingos[$wmpl_langcode] . "&portal=Default" ?>"

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>

IMG SRC Using Path from MySQL and PHP

I have the following line of code which doesn't seem to be working:
echo "<img src='/images/albumart'"; echo $row['art1']; echo "/>";
The 'art1' is definitely returning '/imagename.png' so the URL should be complete however nothing being displayed.
Any ideas?
Thanks.
You really should be concatenating that string:
<?php
echo '<img src="/images/albumart'.$row['art1'].'"/>';
?>
and ideally break out of php to do this:
<img src="/images/albumart<?php echo $row['art1']; ?>"/>
If you're not familiar, the period in php joins strings. That's called concatenation. No need to echo little bits like that, in fact please never do it the way you demonstrate. The root of your issue is, as Geoff pointed out, you weren't closing the src param.
echo "<img src='/images/albumart'"; echo $row['art1']; echo "/>";
It looks like you are closing the img src paramter after the albumart part.,
try this:
echo "<img src='/images/albumart"; echo $row['art1']; echo "' />";

How do I use an IF statement in PHP to decide if I should show an image or not?

I'm setting up a weather page using the wunderground.com API. Anyway, I want to do the following:
Of the variable $weather is equal to Clear, I want to display an image.
I've tried this:
if ($weather=="Clear") echo <img src="http://example.org/clear.gif" alt="Clear"/>
What do I need to do instead?
Try this
if ($weather=="Clear") echo '<img src="http://example.org/clear.gif" alt="Clear"/>';
The code you tried will render ERROR. You need to place the HTML text and any string within quotes, before you echo them.
if ($weather=="Clear") echo '<img src="http://example.org/clear.gif" alt="Clear"/>'
Remaining, there is nothing else to improve :)
if ($weather==="Clear")
echo "<img src=\"http://example.org/clear.gif\" alt=\"Clear\"/>";
echo '<img src="http://example.org/clear.gif" alt="Clear"/>';
OR
echo "<img src='http://example.org/clear.gif' alt='Clear' />";
if ($weather=="Clear") echo "<img src=\"http://example.org/clear.gif\" alt=\"Clear\"/>";
OR
if ($weather==="Clear") echo '<img src="http://example.org/clear.gif" alt="Clear"/>';
OR
if ($weather==="Clear") echo <<<ABC
<img src="http://example.org/clear.gif" alt="Clear"/>
ABC;
andsoon.
For conditionally rendering html, I often just use the php tags...
if($weather === "Clear") {?>
<img src="http://example.org/clear.gif" alt="Clear"/>
<?php}

How to echo html with variables in herf?

I tried to echo the following html, I can see from firebug that the variables are correctly printed, but it doesn't show up on the webpage. Would anyone please help me correct this?
$html ="<p><a rel='friends' href='$domain/$dir/$tag/$version/' target='_blank' $img></a></p>";
Your $img is in the wrong spot:
$html ="<p><a rel='friends' href='$domain/$dir/$tag/$version/' target='_blank'>$img</a></p>";
Then again I think you want to do this:
$html ="<p><a rel='friends' href='$domain/$dir/$tag/$version/' target='_blank'><img src='$img' /></a></p>";
There is nothing between the and the tags. you need something like this:
<p><a rel='friends' href='$domain/$dir/$tag/$version/' target='_blank'> $img </a></p>

Categories