简体   繁体   中英

viewport width in php

I have a php file which I need to modify, but I don't know much of php. Generally speaking what I need to achieve is the effect of this line:

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

but written in php. The above one is in 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. Is there any alternative to this code in php? Thanks in advance

PHP doesnt really come in to play here. 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. This must be done by querying the user agent server variable $_SERVER['HTTP_USER_AGENT'] to detect the correct format for the device.

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:

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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