Reputation: 2368
I'm trying to create an <a>
tag with a PHP function that returns a image source with its html.
My code is currently this:
if (returnString('image')) : echo '<img src="'.returnString('image').'" />'; endif;
In which my intended final output would be:
<img src="http://..." />
But its only returning this:
"http://..."
Which is only the string. Why is this so?
EDIT: I'd actually like to add that I want to check if the function exists (if its null there should be any echo-ing), which is why I have the if
at the start.
It works if I don't check for the function like this:
echo '<img src="'; returnString('image'); echo '" />';
Upvotes: 0
Views: 275
Reputation: 11832
You say it works when you do:
echo '<img src="'; returnString('image'); echo '" />';
This code has 3 parts. The first is echo
and prints <img src="
. The returnString()
function apparently does it's own printing. The last is also echo
and prints " />
.
But you say it wont inside the if
, when you put it like:
echo '<img src="'.returnString('image').'" />';
But do mind that here you don't expect the returnString()
function to do it's own printing. You expect it to just return to you the value to be concatenated. If the function actually does its own printing, as I said above, then you will get the eventual src
contents to be displayed BEFORE the <img>
tag
Upvotes: 0
Reputation: 76666
You don't need the :
and endif
here.
Try this:
if (returnString('image')) echo '<img src="'.returnString('image').'" />';
Alternatively, you could store the return value in a variable (as mplungjan suggested above) and use that in your code (more readable, IMO). That way, you can avoid your function from being called twice.
$src = returnString('image');
if (isset($src)) echo '<img src="'.$src.'" />';
I'd actually like to add that I want to check if the function exists, which is why I have the if at the start.
There's a built-in function, exactly for the same purpose -- function_exists()
You can do the following:
if (function_exists('returnString')) {
$src = returnString('image');
echo '<img src="'.$src.'"/>';
}
Hope this helps!
Upvotes: 2
Reputation: 129
Try this :-
<?PHP
$s="http://www.sagmart.com/image/logoes.png";
if($s) {
echo '<img src="'.$s.'" />';
}
?>
I dont have no idea about you returnString('image') so i used the new variable.
remove the endif in the last.
Upvotes: 0
Reputation: 178421
$str = returnString('image');
if (isset($str)) echo '<img src="'.$str.'" />';
Upvotes: 1