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

Posted by J.O. Aho on 12/02/07 20:37

MangroveRoot wrote:

> ======
> <?php
> echo "<TITLE>";
> echo $title;
> echo "'</TITLE>";
> ?>
This should result in a title: Doesn't Matter'
Not what you really wanted, just drop the single quote at the end tag of title.

> 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.

This for the web browsers parser will see the single quote in your string as
the end for the alt-option and the "t Matter'" as unknown options.
You can use double quotes for the tag options, there are still some
proletarian browsers which has trouble with single quoted tag options.

You seem to be over using the echo function too, why not just

<h1><img src="foo.gif" alt="<?PHP echo $title; ?"></h1>

of course you can use short tags for the php, but these may be removed in php6.


> However, if I try this:
> ======
> <?php echo '
> <H1><IMG SRC="_images/_rock/Zacs World.gif" ALT="$title"></H1>
> '; ?>
> ------

there is a difference between
echo '$omthing';
and
echo "$omething";

When you use single quotes around a string you want to echo, you tell php to
not parse the string at all, just echo it as it is. Double quotes tells that
you want the string to be parsed and variables replaced with the value.

even if
echo "$omething";
works, I would recommend you to use
echo "{$omething}";
as this way you will have less trouble that php parses the wrong variables
(wrong from your point of view).


> 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?

It's not a php problem, but the HTML syntax and as it don't take care of
escapes for quotes, you won't be able to do what you want to do, you need to
change the method you are using to a more sane one.


--

//Aho

 

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

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