help me to fix the newbie problem: double quotes in echo
Moderator: General Moderators
-
php12342005
- Forum Commoner
- Posts: 79
- Joined: Mon Mar 21, 2005 3:35 am
help me to fix the newbie problem: double quotes in echo
I am a C++ programmer and move to php temprately now.
I use braces for echo: echo(), and double quotes for string in possible cases, otherwise I just feel unconfortable.
my purpose is to display a message box in php code.
ok, following is my sick code:
function MsgBox000($str)
{
echo("<script language='javascript'>"); //line 1
second line: discussed in bellow.
echo("</script>"); //line 3
}
(line 1 and 3 are easy)
line 2:
//works but use single quote: echo('...');
echo('alert("'.$str.'");');
now I want to modify single quote to double quote: echo("...");
but got all errors with these 3 tests:
1) echo("alert("$str");");
2) echo("'alert("'.$str.'");'");
3) echo("alert("".$str."");");
(and more tests, I don't want to list all)
do you know correct syntax for echo("...") of alert()?
thanks
I use braces for echo: echo(), and double quotes for string in possible cases, otherwise I just feel unconfortable.
my purpose is to display a message box in php code.
ok, following is my sick code:
function MsgBox000($str)
{
echo("<script language='javascript'>"); //line 1
second line: discussed in bellow.
echo("</script>"); //line 3
}
(line 1 and 3 are easy)
line 2:
//works but use single quote: echo('...');
echo('alert("'.$str.'");');
now I want to modify single quote to double quote: echo("...");
but got all errors with these 3 tests:
1) echo("alert("$str");");
2) echo("'alert("'.$str.'");'");
3) echo("alert("".$str."");");
(and more tests, I don't want to list all)
do you know correct syntax for echo("...") of alert()?
thanks
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Please post your code in
Code: Select all
tags, it's hard to read when you don't.
You'll find you get much better responses when you do.Lets take it step by step.
If $str="foo";
you want to output to the browser
Correct?
Thus
would do the fixed value. You have the escape the quotes as you want them to be sent to the browser, not used as string delimitors in PHP.
Finishing it
Had this been C you might have done something like
Same thing applies, escape the quotes.
If $str="foo";
you want to output to the browser
Code: Select all
alert("e;foo"e;);Thus
Code: Select all
echo "alert(\"foo\");";Finishing it
Code: Select all
echo "alert(\"$str\");";Had this been C you might have done something like
Code: Select all
printf("e;alert(\"e;%s\"e;);"e;,message);
// or
cout &lt;&lt; "e;alert(\"e;"e; &lt;&lt; message &lt;&lt; "e;\"e;);"e;;
Last edited by nielsene on Thu Jul 28, 2005 1:45 pm, edited 1 time in total.
-
php12342005
- Forum Commoner
- Posts: 79
- Joined: Mon Mar 21, 2005 3:35 am
-
php12342005
- Forum Commoner
- Posts: 79
- Joined: Mon Mar 21, 2005 3:35 am
-
php12342005
- Forum Commoner
- Posts: 79
- Joined: Mon Mar 21, 2005 3:35 am