Page 1 of 1

help understanding this line of code...

PostPosted: 08. July 2009 05:17
by hem
echo '<p><a href=" ' . $_SERVER['PHP_SELF'] . ' ?addjoke=1">add a joke!</a></p>';

This line of code is from Build your own database driven book. In the above mentioned code i am not able to understand the use of both single and double quotes. i mean which quote escapes which one? :idea:

Re: help understanding this line of code...

PostPosted: 08. July 2009 07:38
by aldo
None of those escape one another. It is just usually with HTML you use double quotes... To avoid having to escape the double quotes with a backslash, single quotes are used to tell the PHP Parser where the PHP code begins and ends...

Re: help understanding this line of code...

PostPosted: 09. July 2009 13:08
by glitzi85
In PHP you can use both, single and double quotes. Small example:

Code: Select all
$test = 'hem';
echo "Hello $test";
echo 'Hello $test';

Output would be:
Code: Select all
Hello hem
Hello $test

Text inside double quotes will be parsed by PHP, in single quotes not. That means, that single quotes are faster, but double quotes may not blow up your code.

When using HTML inside PHP i prefer single quotes, otherwise you need to escape all the double quotes of the HTML code. But that's something you can decide by yourself.

glitzi