user1043070
user1043070

Reputation: 23

Simple PHP Character Escaping Issue

I am trying to use a php variable for an image...should be easy, right?

But have spent some time looking over the bellow code and cannot see the error. I suspect there is an issue with escaped characters, although cannot put my finger on it.

<?php 
$image=http://www.infiniteplastic.com/wp-content/uploads/2007/03/Hat_Poker.jpg;
echo '<img src="$image" class="bg">' ;
?>

Appears as...

' ; ?> 

It looks like the php stops after "bg"> and reads the following four characters as html, but am not sure why it would do that. Any suggestions will be much appreciated. Thank You.

Upvotes: 0

Views: 224

Answers (5)

Evan Harrison
Evan Harrison

Reputation: 356

There's a couple things wrong here. I'm going to try and make it clearer for you:

<?php 
$image = "http://www.infiniteplastic.com/wp-content/uploads/2007/03/Hat_Poker.jpg";
echo "<img src='".$image."' class='bg' alt='dont forget alt tag' />" ;
?>
  • Missing quotes around variable
  • img tag needs to be self-closing
  • src still needs quotes around it's link

Tried this on my server and it works.

Upvotes: 0

wargodz009
wargodz009

Reputation: 305

<?php $image='your image link here. sorry i cant post image link just because im new here'; 
echo "<img src='$image' class='bg'>" ; 
?>

this solves your problem. ive tried that in my localhost.

your problem is because of the ' and the " you must learn how to use that properly.

Upvotes: 0

Moe Sweet
Moe Sweet

Reputation: 3721

First, wrap the string in quotes

$image='http://www.infiniteplastic.com/wp-content/uploads/2007/03/Hat_Poker.jpg';

and...

You're wrapping a variable in single quotes. Cannot do it.

wrong

echo '<img src="$image" class="bg">' ;

right

echo '<img src="'.$image.'" class="bg">' ;

Upvotes: 2

Shomz
Shomz

Reputation: 37701

Don't forget the quotes!

$image='http://www.infiniteplastic.com/wp-content/uploads/2007/03/Hat_Poker.jpg';

Upvotes: 0

codaddict
codaddict

Reputation: 455020

Missing quotes around the string:

$image='http://www.infiniteplastic.com/wp-content/uploads/2007/03/Hat_Poker.jpg';
       ^                                                                       ^

Upvotes: 0

Related Questions