-1

I am aware that I cannot use an echo inside an echo. My php code is:

echo '<img src="img/image1.jpg"

I want to use php variable as the source. Somethink like:

echo '<img src="php-code"
user3192304
  • 337
  • 1
  • 3
  • 10
  • 2
  • 3
    Do try to learn basic php before asking on SO. – Epodax Jun 03 '16 at 12:53
  • @Epodax Be a bit supportive. He is obviously in the very basics of scripting and it could be tough to formulate Google queries and find correct place to look for such a thing. – Josef Sábl Jun 03 '16 at 13:01
  • 2
    @JosefSábl There are PLENTY of tutorials / guides / step-by-steps out there, in more than one language, SO is not a place where you learn the basics, you come here if you have a issue with specific code. - If you hover over the down vote button you'd literally see a "Shows no research effort" – Epodax Jun 03 '16 at 13:03
  • 1
    Although you should learn the basics, how about `echo ""` ? –  Jun 03 '16 at 13:27
  • Possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – Jocelyn Jun 03 '16 at 14:00

5 Answers5

1

Using .(dot) you can concatenate php variable in echo statement.

echo '<img src="'.$src.'" />';
Dhara Parmar
  • 8,021
  • 1
  • 16
  • 27
1

You have four options:

$url = '...';

//1
echo '<img src="' . $url . '">';

//2
echo "<img src='{$url}'>";        //Note that you have to use double quotes

//3
echo '<img src="';
echo $url;
echo '">';

//4
echo '<img src="', $url, '">';    //I would not recommend this one though
Josef Sábl
  • 7,538
  • 9
  • 54
  • 66
0

just skip first echo and write the html with an echo in the middle

<img src="<?=$src?>">
l0rdseth
  • 89
  • 9
0

All of these works

echo '<img src="', $url, '">'; # This works by sending 3 different parameters to echo
echo '<img src="' . $url . '">'; # This works by concatenating 3 strings before echoing

echo '<img src="'; echo $url; echo '">'; # This works by echoing 3 strings in turn

echo "<img src=\"$url\">";   # All of these works by inserting
echo "<img src=\"${url}\">"; # the value of $url in the string
echo "<img src=\"{$url}\">"; # before echoing. " needs to be escaped.

# And finally, this (called HEREDOC) does the same thing as above
# only without ", so that sign is not needed to be escaped
echo <<<FOO
    <img src="$url">
    <img src="{$url}">
    <img src="${url}">
FOO;
Xyz
  • 5,955
  • 5
  • 40
  • 58
0

There are many solutions. I prefer this one: <?php echo '<img src="'.$url.'">'; ?> $url stands for the Image-Url, I guess you know ist.

You can do it also: For example: <img src="<?php echo $url; ?>"> But I like the first method, it's a simple way to put out strings.

FileX
  • 11
  • 4