问题
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!
回答1:
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']);
来源:https://stackoverflow.com/questions/22468325/php-server-not-working-properly