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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
php12342005
Forum Commoner
Posts: 79
Joined: Mon Mar 21, 2005 3:35 am

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

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
Last edited by nielsene on Thu Jul 28, 2005 1:45 pm, edited 1 time in total.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

seriously ditch the () and just do

echo '<script>alert("'.$var.'")</script>';
php12342005
Forum Commoner
Posts: 79
Joined: Mon Mar 21, 2005 3:35 am

Post 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.
php12342005
Forum Commoner
Posts: 79
Joined: Mon Mar 21, 2005 3:35 am

Post 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:
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

You don't need to escape single quotes inside double quotes, or vice versa.
php12342005
Forum Commoner
Posts: 79
Joined: Mon Mar 21, 2005 3:35 am

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