php将url与变量结合起来

I want to do define the following variable $url

$url = www.example.com/$link;

where $link is another predefined variable text string e.g. testpage.php

But the above doesn't work, how do I correct the syntax?

Thanks

Try this:

$url = "www.example.com/$link";

When string is in double quotes you can put variables inside it. Variable value will be inserted into string.

You can also use concatenation to join 2 strings:

$url = "www.example.com/" . $link;

Needs double quotes:

$url = "www.example.com/$link";

It'd be helpful if you included the erroneous output, but as far as I can tell, you forgot to add double quotes:

$url = "www.example.com/$link";

You will almost certainly want to prepend "http://" to that url, as well.

Alternate way:

$url = "www.example.com/{$link}";
$url = "www.example.com/$link";

Hate to duplicate an answer, but use single quotes to prevent the parser from having to look for variables in the double quotes. A few ms faster..

$url = 'www.example.com/' . $link;

EDIT: And yes.. where performance really mattered in an ajax backend I had written, replacing all my interpolation with concatenation gave me a 10ms boost in response time. Granted the script was 50k.