简体   繁体   English

PHP中的视口宽度

[英]viewport width in php

I have a php file which I need to modify, but I don't know much of php. 我有一个需要修改的php文件,但是我对php的了解不多。 Generally speaking what I need to achieve is the effect of this line: 一般来说,我需要实现的是以下行的效果:

    <meta name="viewport" content="width=480" />

but written in php. 但是用PHP写的。 The above one is in html. 上面的是html。 I'm trying to match the resolution to fill the whole screen of mobile devices, that's why I set a width of 480 pixels. 我试图匹配分辨率以填充整个移动设备的屏幕,这就是为什么我将宽度设置为480像素。 Is there any alternative to this code in php? 在php中,此代码还有其他选择吗? Thanks in advance 提前致谢

PHP doesnt really come in to play here. PHP并没有真正发挥作用。 What you want is: 您想要的是:

<meta name="viewport" content="width=device-width"/>

PHP will output the text you have specified from an echo, however it sounds like you are trying to set the width from PHP. PHP将输出从回显中指定的文本,但是听起来您正在尝试从PHP设置宽度。 This must be done by querying the user agent server variable $_SERVER['HTTP_USER_AGENT'] to detect the correct format for the device. 这必须通过查询用户代理服务器变量$ _SERVER ['HTTP_USER_AGENT']来完成,以检测设备的正确格式。

However a better way we use is to do the detection in the HTML (ignore the PHP), and dynamically add the viewport tag from JS like such: 但是,我们使用的更好的方法是在HTML中进行检测(忽略PHP),并从JS中动态添加视口标签,如下所示:

<head>
<script>
if(iPhone)
{
      document.write("<meta name="viewport" content=\"width=480\""+"/>");
}
else // iPad
{
     document.write("<meta name="viewport" content=\"width=1024\""+"/>");
}
</script>
</head>

This needs no server side processing and allows you the chance to dynamically query the DOM about the actual display, something PHP cannot know. 这不需要服务器端处理,并且使您有机会动态查询有关实际显示的DOM,这是PHP无法知道的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM