PHP $_Server 无法正常工作 [英] PHP $_Server Not Working Properly

查看:46
本文介绍了PHP $_Server 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下几行:

一切都很好,它显示了当前链接,比方说:http://www.example.com/mypage.php

现在我想将它与一个变量相关联

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

当我回应这个时,我得到http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"——换句话说就是代码本身.我究竟做错了什么?

我也试过:$myURL = '{http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]}'

我也试过直接回显出来:

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

我忽略了什么?谢谢!

解决方案

变量在单引号中时不会内插.使用双引号:

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

或者,为了清楚起见:

$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']);

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']);

这篇关于PHP $_Server 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆