I have the following line:

<div class='social'>http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]</div>

It's all great and it displays the current link, let's say: http://www.example.com/mypage.php

Now I want to associate this with a variable as such

$myURL = 'http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]'

When I echo this out I get "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" - in other words the code itself. What am I doing wrong?

I've also tried: $myURL = '{http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]}'

I've also tried echoing it out directly:

echo 'http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]';

What am I overlooking? Thank you!

有帮助吗?

解决方案

Variables are not interpolated when in single quotes. Use double quotes:

$myURL = "http://$_SERVER['HTTP_HOST']$_SERVER['REQUEST_URI']";

Or, for clarity:

$myURL = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$myURL = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$myURL = sprintf("http://%%",$_SERVER['HTTP_HOST],$_SERVER['REQUEST_URI']);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top