1

I am trying to echo an image in PHP however I am only achieving the URL. This is using the Instagram API.

echo $pics['data'][0]['images']['standard_resolution']['url'];

Whats confusing me is where to use the <img src= /> without the code breaking.

3 Answers 3

7
echo "<img src=\"".$pics['data'][0]['images']['standard_resolution']['url']."\">";
1
  • It's funny how this line of code is an answer to some question.
    – dlock
    Mar 4, 2013 at 18:48
5

You will want to use the complex syntax for outputting values from complex variable types into double quoted and parsed strings.

Example:

echo "<img src='{$pics['data'][0]['images']['standard_resolution']['url']}'>";

Output:

<img src='http://nicenicejpg.com/350/150'>

php codepad of example

From the PHP documentation on Strings, and more specifically Double Quoted strings.

Any scalar variable, array element or object property with a string representation can be included via this syntax. Simply write the expression the same way as it would appear outside the string, and then wrap it in { and }.

I believe this method of including strings adds to readability when there would otherwise be no advantage to assigning the value to a simple variable prior to it's use in a string.

4

Assuming that $pics['data'][0]['images']['standard_resolution']['url']; refers to an image url, you'll want to do something like...

<? $url = $pics['data'][0]['images']['standard_resolution']['url'];
...
echo "<img src=\"".$url."\">";
?>

This does the SAME thing that Duniyadnd suggested, but makes the output "part" a bit more readable.

1
  • Thanks, I'm sure both answers are correct - It may be something on my part why they aren't showing up. I will continue to plug, play and fix. Its appreciated
    – bruh
    Mar 5, 2013 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.