【发布时间】:2016-10-04 05:58:41
【问题描述】:
这可能看起来微不足道,但 intval() 在以下情况下返回 0:
$window_width = "<script type='text/javascript'>document.write(window.innerWidth);</script>";
$window_width2 = intval($window_width);
echo $window_width2;
$window_width 将是 "1366"。
$window_width2 将是 0。
我需要 $window_width2 是 integer,而不是 string。
【问题讨论】:
-
intval — Get the integer value of a variable,请在此处查看> php.net/manual/en/function.intval.php 并且您的$window_width不包含整数值 -
您还会发现您将客户端脚本与服务器端执行混合在一起。例如,
window.innerWidth将在页面呈现后可用,之后 php 已经完成了一段时间。因此,它无法使用该值,而只能使用 ajax 之类的东西,以及不太理想的数据库。 -
intval 返回整数值。它不会将字符串转换为整数
-
@OwaisArain - 你误会了它会将字符串转换为整数,例如
intval('1')确实会变成1但是<script></script>不是数字,然后变成0 -
我想我已经掌握了窍门。非常感谢!
标签: javascript php string integer