PHP $_Server Not Working Properly [duplicate]

允我心安 提交于 2019-12-04 05:35:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!