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
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;
Double quote required:
$url = "www.example.com/$link";
Alternative way:
$url = "www.example.com/{$link}";
$url = "www.example.com/$link";
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.
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.