我想在 php 中将 Javascript 变量转换为 INT。
示例:
$res = '<script> var res = Math.max(screen.width,screen.height); document.write(res); </script>';
$int = (int)$res;
var_dump($int);
此代码返回 int(0) 或字符串中的值,我需要正确的整数值。 例如 1920 年。
最佳答案
当你使用(int)
时,php会尝试将给定的字符串转换为整数,就好像它以数字开头一样,它将使用这个数字,例如:
(int) "123cats";//123
你怎么没有任何数字,php 结果只是给你一个 0
。
您不能以这种方式将 javascript 变量传递给 php 脚本。
如果您想这样做,则必须在 ajax
请求或某种形式的请求中传递它。
关于javascript - Javascript 变量到 PHP int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40470795/