Home > Backend Development > PHP Tutorial > 诡谲,这句PHP看起来都对,可是无法得到正确结果

诡谲,这句PHP看起来都对,可是无法得到正确结果

WBOY
Release: 2016-06-13 11:21:20
Original
900 people have browsed it

诡异,这句PHP看起来都对,可是无法得到正确结果。

<br /><br />$screen_w = "<script>document.write(screen.width);</script>";<br /><br />if($screen_w < 500){<br />echo '宽度小于500';<br />}<br />else{<br />echo '大于等于500';<br />}<br /><br />
Copy after login


直接 echo $screen_w; 可以得到正确结果是 1366

if判断的时候  结果却是  1366小于500

如果 (int)$screen_w   或者  intval($screen_w)

直接显示为0了

何解?


------解决方案--------------------
没有语法错误,就表示是对的吗?

1、php 先于 js 执行!即便是 $screen_w = "<script>document.write(screen.width);</script>"; 能得到数据,那也是 php 代码执行完了以后
2、$screen_w = "<script>document.write(screen.width);</script>";
是什么呢?只不过是个字符串赋值
即便有 echo $screen_w;
那也只是在浏览器中显示而已
------解决方案--------------------
var_dump打印出来,是46位的字符串,JS代码也算在里面了,用正则把数字匹配出来再比较就可以了。
------解决方案--------------------
首先要明白哪些是客户端的代码,哪些是服务端的代码,二服务端的代码是优先执行的,客户端的是后执行的
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template