Sunday 18 March 2007

PHP: escaping double quotes

In PHP you sometimes have double quotes (this one: " ) in a string. Because double quotes also indicate the start and end of the string you have to escape them when they are within the string. Today I had the following example. I needed to put a piece of HTML to show an image into a string. So I tried:

$prev = "<img src="leftgr.GIF" width="13" height="13" border="0" >";

ofcourse this did notwork as PHP thinks the string ends with the second double quote "<img src=". It should be:

$prev = "<img src=\"leftgr.GIF\" width=\"13\" height=\"13\" border=\"0\" >";

The backslash (this one: \) escapes characters. PHP knows then then the character does not belong to the PHP code but is just a character in a string.

My website: www.dejongfotografie.nl

No comments: