-4

I'm using CodeIgniter to insert an image into an empty div tag and I'm having some trouble putting php code within a an echo. I've been trying to figure it out but I just can't seem to get my head around why it's not working.

<?php
     echo ('<div class="badge"><img src="<?php echo base_url("images/profiles/badge100.png"); ?>" title="100% on a quiz!" alt="100% on a quiz!"/></div>');
?>

I thought using "..." inside of '...' was the right thing to do. But obviously not. The following is what it's coming out as on the actual HTML page.

 <div class="badge">
          <img src="?&gt;&lt;?php echo base_url(" images="" profiles="" badge100.png");="" ?=""><!--?php " title="100% on a quiz!" alt="100% on a quiz!"/-->
 </div>

I appreciate your help!

Lewis.

EHU-Lewis
  • 75
  • 1
  • 1
  • 10

4 Answers4

1

Why do you want to use echo() inside echo()? You can simply use (variable) in place substitution .

<?php
     $base = base_url("images/profiles/badge100.png");

     echo ('<div class="badge"><img src="{$base}" title="100% on a quiz!" alt="100% on a quiz!"/></div>');
?>
Fawzan
  • 4,738
  • 8
  • 41
  • 85
  • This would not work. Variables inside of single-quoted strings do not evaluate. I wonder who voted this up. – Daedalus Jan 06 '14 at 00:52
1

You just have to break the echo string, additionally I wouldn't use echo with (). Add values with the dot, use single quotes, so you can use double quotes in html.

$a = "apples";
$b = "bananas";
echo '<p class="exampleClass">Hmmm I like '.$b.' and '. $a.' too,</p>';
user3135691
  • 151
  • 8
1

You would use php operator "." simple example is right here

<?php 
    echo '<div class="badge"><img src="'. base_url("images/profiles/badge100.png") . '/></div>';
?>
Filip Lukáč
  • 100
  • 1
  • 2
  • 13
0

Do this:

<?php
 echo ('<div class="badge"><img src="'.base_url("images/profiles/badge100.png").'" title="100% on a quiz!" alt="100% on a quiz!"/></div>');
?>
user2936213
  • 1,021
  • 1
  • 8
  • 19