简体   繁体   English

PHP包含基于屏幕大小的文件

[英]PHP include file based on screen size

        <?php

        include 'components/server.php';

Is it possible to make it include server.php for desktops and server-mobile.php for mobile devices?是否可以让它包含用于桌面的 server.php 和用于移动设备的 server-mobile.php?

While technically possible, it's absolutely not the best way of doing things.虽然在技术上是可行的,但这绝对不是最好的做事方式。

Why?为什么?

Because PHP runs on the server and only the output of that PHP execution is given to the browser.因为 PHP在服务器上运行,并且只有 PHP 执行的输出提供给浏览器。 You would probably be wanting something using javascript which can load and then seamlessly react to the browser conditions, such as screen size and/or dimensions.您可能想要使用javascript的东西,它可以加载然后无缝响应浏览器条件,例如屏幕大小和/或尺寸。

If you're trying to change which PHP script is running based on the browser criteria (as mentioned above) this sounds very much like your programming logistics are simply wrong.如果您尝试根据浏览器标准(如上所述)更改正在运行的 PHP 脚本,这听起来很像您的编程逻辑完全错误。

If you somehow really do need to change PHP script execution based on end-client (browser) characteristics you could do this by calling a script based on javascript AJAX or using mechanisms mentioned in comments above, but as said, you're almost certainly "doing it wrong".如果您确实确实需要根据终端客户端(浏览器)特性更改 PHP 脚本执行,您可以通过调用基于javascript AJAX的脚本或使用上面评论中提到的机制来做到这一点,但如前所述,您几乎可以肯定“做错了”。

Alternative选择

It would be far better to load everything you need in PHP and then pass all of that content to the browser (as output; HTML, CSS, Javascript, etc.) for the Javascript in the browser to then decide which parts of the data it needs to use and ignoring the others.最好在 PHP 中加载您需要的所有内容,然后将所有这些内容传递给浏览器(作为输出;HTML、CSS、Javascript 等),以便浏览器中的 Javascript 然后决定它的数据的哪些部分需要使用并忽略其他。

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

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