双引号内的单引号

i read string documentation on PHP and found out that Single quoted strings will display things almost completely "as it is." Variables and most escape sequences will not be interpreted except \' and \\

I wanted to display a hyperlink whose address should be http://localhost/kk/insert.php/?id="4"

i tried the following code

$id = 4;
echo "<a href='http://localhost/kk/insert.php/?id=".$id."'>edit</a>";

But it's displaying http://localhost/kk/insert.php/?id=4 (there are no double quotes surrounding 4)

However, i accomplished the result by using

echo "<a href='http://localhost/kk/insert.php/?id=\"$display_result\"'>edit</a>";

My question is that single quotes does interpret \" escape character. So why the first code is not displaying double quotes (that are placed inside single quotes). What am i missing?

You shouldn't have quotes around the integer. Your url should be http://localhost/kk/insert.php/?id=4 which is accomplished using the following code:

$id = 4;
echo '<a href="http://localhost/kk/insert.php/?id='.$id.'">edit</a>';

You're dealing with TWO languages there. PHP is doing the echo, and the " quotes are parsed/removed by PHP. Then there's the ' quotes, which are used in the HTML to delimit the href attribute.

With your escaped second version:

echo "<a href='http://localhost/kk/insert.php/?id=\"$display_result\"'>edit</a>";
     ^--php   ^--html                             ^^--escaped for PHP

Normally that " before $display_result would TERMINATE the PHP string you've been echoing. But since it's been escaped (\"), the escape tells PHP to treat that quote as plaintext, and NOT as a quote. So the PHP string continues, and when this code actually executes and is output from your server, the browser will actually see:

<a href='http://localhost/kk/insert.php/?id="XXX"'>edit</a>

To do it with your first example, just do :

$id = 4;

echo "<a href='http://localhost/kk/insert.php/?id=\"".$id."\"'>edit</a>";

The interpretting difference between single quote and double quote you found is this:

$a = 4;
echo '$a' . "$a"; // $a4
// '$a' just prints `$a`
// "$a" prints `4`, it's interpretted
// alternatively "\$a" prints `$a`

As for the escaping. If your string delimiter is a single quote then you don't need to escape double quotes, and vice versa.

$a = "don't";
// vs
$a = 'don\'t';

$a = '"quote"';
// vs
$a = "\"quote\"";