You are here: single quote in string constant « PHP Language « IT news, forums, messages
single quote in string constant

Posted by MangroveRoot on 12/02/07 19:41

I've looked in various manuals and tutorials,
and they cover the obvious situations, but not this one:

At the very top of my .php file, I define a variable as follows:
======
$title = "Doesn't Matter";
------
Note the apostrophe (or, single quote) in the string value.

Further down, in the <HEAD>...</HEAD>, I have the following:
======
<?php
echo "<TITLE>";
echo $title;
echo "'</TITLE>";
?>
------
If I bookmark the page, the name is what I hoped: "Doesn't Matter".

Further down, in the <BODY>...</BODY>, I have the following:
======
<?php echo "
<H1><IMG SRC='foo.gif' ALT='$title'></H1>
"; ?>
------
This fails, producing only "Doesn" in whatever format corresponds to H1.
I'm guessing that's because the singlequote in the variable
is somehow interacting with the singlequotes in the echoed string.

However, if I try this:
======
<?php echo '
<H1><IMG SRC="_images/_rock/Zacs World.gif" ALT="$title"></H1>
'; ?>
------
(which I perhaps prefer,
because the HTML looks the way I would like it to look)
this also fails, producing "$title" in whatever format corresponds to H1.

Is there some way I can "escape" or "quote" the singlequote in the variable
so that it will just be taken as a value (I guess)
rather than as something to be concatenated with what's around it
and thus interacting with the quotes around it?

I've tried doubling it and prefacing it with backslash,
but that just displays (if at all) as the same thing
only with an extra quote or with a backslash.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация