Jump to content


problem with echo " and '


3 replies to this topic

#1 ht56y

    Young Padawan

  • Members
  • Pip
  • 3 posts

Posted 11 September 2007 - 09:52 AM

Hi

I have a image gallery, in which i for each cell use a script to change the background of the cell, and it works fine as long as i keep it in html.

But i'm converting my gallery to php now, and im getting a problem with the code.

When i echo in php, i use " to enclose the echoed statements, and convert all statements within to use ' instead of ".

This makes the code to change the cell bg not to react on rollover

Example:

When i echo this in php, it dosen't have the rollover. it works fine in plain html:
Quote: echo "<td height='80' onmouseover="this.style.backgroundColor='#676c58'; " onmouseout="this.style.backgroundColor='#383b30';" align='center'><a href='#'><img src='Billeder/Thumbs/tmb05.jpg' width='70' height='70' border='0' /></a></td>" I think it's a problem with the ' and the " signs.

Do you have a solution or a work around, it would be much appreciated.

Thanks in advance.

#2 .CJ

    Young Padawan

  • Members
  • Pip
  • 114 posts
  • Gender:Male
  • Location:Leeds, UK

Posted 11 September 2007 - 01:10 PM

When I use HTML inside an echo function, I do it like so...

echo('Hello, my name is <span style="font-color: red">Chris</span>, how are you?');

If you use double quotes instead of single quotes, you need to add a forward slash (\) before every double quote in your HTML. Just use single quotes, it's much easier.

#3 ht56y

    Young Padawan

  • Members
  • Pip
  • 3 posts

Posted 11 September 2007 - 01:15 PM

That solved it, thanks

#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 11 September 2007 - 01:35 PM

For the sake of learning, the problem is here:

Quote

echo "<td height='80' onmouseover="this.style.backgroundColor='#676c58'; " onmouseout="this.style.backgroundColor='#383b30';" align='center'><a href='#'><img src='Billeder/Thumbs/tmb05.jpg' width='70' height='70' border='0' /></a></td>"
You need to escape the double quotes using a backslash. Of course, my prefered method is like CJ, to use single quotes around the html.

ref: http://php.net/types.string

Edited by rc69, 11 September 2007 - 01:36 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users