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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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 ================== |
|
|||
|
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 |
|
|||
|
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't Matter"; |
|
|||
|
Allodoxaphobia wrote:
>> >> At the very top of my .php file, I define a variable as follows: >> ====== >> $title = "Doesn't Matter"; > > Not tested: > > $title = "Doesn'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.) |
|
|||
|
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? |
|
|||
|
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 ================== |
|
|||
|
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. |
|
|||
|
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 |
![]() |
| Thread Tools | |
| Display Modes | |
|
|