(Aug 10, 2018 07:31 AM)pr0slyte.. Wrote: I have
Code
$images = json_decode('{"small": "path-to-small", "large": "path-to-large"}');
When I want to print the path of image small or image large I'm using this:
Code
print $images->{'small'};
print $images->{'large'};
But how to print the path in echo, for example:
Code
echo '<a href="LARGE HERE" data-lightbox="mygallery" title=""><img src="SMALL HERE" alt=""></a>';
I think you get the point!
Thank you!
You're decoding it correctly. What you're trying to do is string concatenation though. PHP supports this in a number of ways to do this.
Concatenation Operator
PHP Code
<?php
$images = json_decode('{"small": "path-to-small", "large": "path-to-large"}');
echo 'Path to small: ' . $images->small
String Interpolation - Simple
PHP Code
<?php
$images = json_decode('{"small": "path-to-small", "large": "path-to-large"}');
echo "Path to small: $images->small";
String Interpolation - Complex
PHP Code
<?php
$images = json_decode('{"small": "path-to-small", "large": "path-to-large"}');
echo "Path to small: {$images->small}";
There are others, but these three will handle most use cases. Concatenation is what you're likely going to use in your specific example. At least as it's structured now. When you need to content with quotations in your output you need to decide between concatenation and interpolation. For interpolation to work you need to use double quotes; PHP won't interpret what is inside a single quote string literal. But if you switch to using double quote strings, you need to escape any occurrence of double quotes in the output.
Another alternative, though overkill for your example, is the use of functions such as
printf() or
sprintf(). They are basically identical to each other; the difference being that
printf() will immediately output the formatted string. You can store the output of
sprintf() to a variable.