Page 1 of 1

help me to fix the newbie problem: double quotes in echo

Posted: Thu Jul 28, 2005 1:33 pm
by php12342005
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

Posted: Thu Jul 28, 2005 1:40 pm
by John Cartwright
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.

Posted: Thu Jul 28, 2005 1:42 pm
by nielsene
Lets take it step by step.

If $str="foo";
you want to output to the browser

Code: Select all

alert(&quote;foo&quote;);
Correct?

Thus

Code: Select all

echo "alert(\"foo\");";
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

Code: Select all

echo "alert(\"$str\");";

Had this been C you might have done something like

Code: Select all

printf(&quote;alert(\&quote;%s\&quote;);&quote;,message);
// or
cout &amp;lt;&amp;lt; &quote;alert(\&quote;&quote; &amp;lt;&amp;lt; message &amp;lt;&amp;lt; &quote;\&quote;);&quote;;
Same thing applies, escape the quotes.

Posted: Thu Jul 28, 2005 1:44 pm
by shiznatix
seriously ditch the () and just do

echo '<script>alert("'.$var.'")</script>';

Posted: Thu Jul 28, 2005 2:27 pm
by php12342005
thanks for all, especially for nielsene,

I will not call myself ;newbie" again in next post.

I do think double quotes are much much better than single quote for echo.

great!

cheers.

Posted: Thu Jul 28, 2005 2:47 pm
by php12342005
unfortunately, problem comes again:

//works
echo("alert(\"$str\");");

but this modification is not working
echo("alert(\'$str\');");

alert("Hello");
and
alert('Hello');
both are OK in script, why my modification is not working?




:roll: :roll: :arrow: :?: :?: :?: :?: :?: :?: :?: :twisted: :twisted: :roll: :twisted:

Posted: Thu Jul 28, 2005 2:52 pm
by nielsene
You don't need to escape single quotes inside double quotes, or vice versa.

Posted: Thu Jul 28, 2005 3:14 pm
by php12342005
ok,
it works.
echo("alert('$str');");

that is really interesting.

above string should be script string.
for php language, must escape apply to single quote, mustn't?
//error
"first 'middle' last"
//ok
"first \'middle\' last"

i need to read a book from first page to last page.