single quote in string constant

This is a discussion on single quote in string constant within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I've looked in various manuals and tutorials, and they cover the obvious situations, but not this one: At the ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-02-2007
MangroveRoot
 
Posts: n/a
Default single quote in string constant

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.
Reply With Quote
  #2 (permalink)  
Old 12-02-2007
Ian Hobson
 
Posts: n/a
Default Re: single quote in string constant

MangroveRoot wrote:
> I've looked in various manuals and tutorials,
> and they cover the obvious situations, but not this one:
>
>
>
> 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?

Indeed there are many ways to do what you want.

I won't repeat the manual here - it does a reasonable job of explaining
the options - its just that there are so many it gets complicated.

What you need is either to escape the double quotes, and let the double
quote processing convert $var into its value. Here you echo a single
processed string, thus.

<?php echo "
<H1><IMG SRC=\"_images/_rock/Zacs World.gif\" ALT=\"$title\"></H1>
"; ?>

Or you can use single quotes, and the concatenation operator dot (.)
which joins the strings it appears between. This means you echo the join
of three strings thus.

<?php echo '
<H1><IMG SRC="_images/_rock/Zacs World.gif" ALT="'.$title.'"></H1>
'; ?>

Regards

Ian


Reply With Quote
  #3 (permalink)  
Old 12-02-2007
Jerry Stuckle
 
Posts: n/a
Default Re: single quote in string constant

MangroveRoot wrote:
> 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.


This isn't a PHP problem. Look at your page source and you'll see why
it's occurring.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
  #4 (permalink)  
Old 12-02-2007
J.O. Aho
 
Posts: n/a
Default Re: single quote in string constant

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
Reply With Quote
  #5 (permalink)  
Old 12-02-2007
Allodoxaphobia
 
Posts: n/a
Default Re: single quote in string constant

On Sun, 02 Dec 2007 19:41:26 GMT, MangroveRoot wrote:
> 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";


Not tested:

$title = "Doesn&apos;t Matter";
Reply With Quote
  #6 (permalink)  
Old 12-02-2007
MangroveRoot
 
Posts: n/a
Default Re: single quote in string constant

Allodoxaphobia wrote:
>>
>> At the very top of my .php file, I define a variable as follows:
>> ======
>> $title = "Doesn't Matter";

>
> Not tested:
>
> $title = "Doesn&apos;t Matter";


This helps a great deal.
A *little* ugly, but only ugly in one place, not *everywhere*.
The explanation (as I see it) is that this ensures that
the punctuation mark indicating the contraction of "does not"
is an apostrope (which, grammatically, it's *supposed* to be)
and not just a singlequote
(which *happens* to look the same in most, if not all, fonts).

I then went on to use this:
======
<?php echo "
<H1><IMG SRC='_images/_rock/Zacs World.gif' ALT='{$title}'></H1>
"; ?>
------
and it worked perfectly, demonstrating that the apostrophe
is not mistaken for a singlequote, and the two do not interact.

I went on further to use this:
======
<?php echo "
<H1><IMG SRC=\"foo.gif\" ALT=\"{$title}\"></H1>
"; ?>
------
This way, the *generated* HTML has the more pleasing
(and, I gather, more backward-compatible) format.
The business of escaping all the doublequotes inside the quoted string
*does* get old after a while -- tiresome to do, prone to error, and ugly --
but it seems to be what most scripting languages do.

(Perl, sed, TECO, and probably others
allow the programmer to specify an alternate character
to quote the entire string, so that ordinary single *or* double quotes
may be used inside the string with impunity.)
Reply With Quote
  #7 (permalink)  
Old 12-02-2007
MangroveRoot
 
Posts: n/a
Default Re: single quote in string constant

MangroveRoot wrote:
> At the very top of my .php file, I define a variable as follows:
> ======
> $title = "Doesn't Matter";
> ------
> (. . .)
> 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?
> (. . .)


BTW, which of these groups --
alt.comp.lang.php, alt.php, comp.lang.php --
is the correct one for questions of this sort?
Reply With Quote
  #8 (permalink)  
Old 12-03-2007
Jerry Stuckle
 
Posts: n/a
Default Re: single quote in string constant

MangroveRoot wrote:
> MangroveRoot wrote:
>> At the very top of my .php file, I define a variable as follows:
>> ======
>> $title = "Doesn't Matter";
>> ------
> > (. . .)
>> 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?
> > (. . .)

>
> BTW, which of these groups --
> alt.comp.lang.php, alt.php, comp.lang.php --
> is the correct one for questions of this sort?
>


None of them. This is an HTML problem.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Reply With Quote
  #9 (permalink)  
Old 12-03-2007
MangroveRoot
 
Posts: n/a
Default Re: single quote in string constant

J.O. Aho wrote:
> 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.


Yeah, I caught that only after my original post.
The extra singlequote in that place didn't really hurt anything,
but it prevented the desired result, and muddied the waters as well.
As you say, "just drop the single quote", and I'm golden.


> You seem to be over using the echo function too, why not just
> <h1><img src="foo.gif" alt="<?PHP echo $title; ?"></h1>


Well, yeah. I'm just starting out, and cutting and pasting
and editing examples. I'm not sure (yet) whether I like embedding PHP
in the middle of HTML tags like that, because it tends to get lost.
But then, if an HTML tag (like <img ...> starts to get long,
I typically format it very formally over several lines, e.g.
<img src="foo.gif"
alt="<?PHP echo $title; ?"
border="blah blah">
(BTW, there's an error here: The end of the alt tag is
?"
but should be
?>"
)


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


If you would direct me to a site that explains short tags,
I would be glad of it,
although it sounds like a habit I don't want to get into.


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


Yeah, I was afraid of something like that, but I'm glad you confirmed it.

Reply With Quote
  #10 (permalink)  
Old 12-03-2007
Michael
 
Posts: n/a
Default Re: single quote in string constant

MangroveRoot wrote:
> - SNIP -
> (Perl, sed, TECO, and probably others
> allow the programmer to specify an alternate character
> to quote the entire string, so that ordinary single *or* double quotes
> may be used inside the string with impunity.)


echo <<<EOI
<h1><img src="_images/_rock/Zacs+World.gif" alt="{$title}"></h1>
EOI
;

You could use the heredoc syntax?

However I only recommend it when you are using many single and double
quotes. In your example your string consists of only double quotes. I
would suggest doing:

echo '<h1><img src="_images/_rock/Zacs+World.gif" alt="' . $title .
'"></h1>';

You should consider using a template engine to keep business logic and
design separate. I recommend Smarty (smarty.php.net).

- Michael
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 09:30 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0