Php concatenating url with variable

I want to define the following $ url variable

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

      

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

But above doesn't work, how can I fix the syntax?

thank

+2


source to share


6 answers


Try the following:

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

      

When the string is in double quotes , you can put variables in it. The variable value will be inserted into the string.



You can also use concatenation to join two strings:

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

      

+11


source


Double quote required:



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

      

+1


source


Alternative way:

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

      

+1


source


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

      

+1


source


Hate to duplicate the answer, but use single quotes to prevent the parser from looking for variables in double quotes. A few minutes faster.

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

      

EDIT: And yes .. where performance really mattered in the ajax backend I wrote replacing all my interpolation with concatenation gave me 10ms in response to response time. The script provided was 50k.

+1


source


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

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

      

You will almost certainly want to add "http: //" to this URL.

0


source







All Articles