Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript PHP intval()返回0_Javascript_Php_String_Integer - Fatal编程技术网

Javascript PHP intval()返回0

Javascript PHP intval()返回0,javascript,php,string,integer,Javascript,Php,String,Integer,这可能看起来微不足道,但在以下情况下,intval()返回0: $window_width = "<script type='text/javascript'>document.write(window.innerWidth);</script>"; $window_width2 = intval($window_width); echo $window_width2; $window\u width=“document.write(window.innerWidth

这可能看起来微不足道,但在以下情况下,
intval()
返回0:

$window_width = "<script type='text/javascript'>document.write(window.innerWidth);</script>";
$window_width2 = intval($window_width);
echo $window_width2; 
$window\u width=“document.write(window.innerWidth);”;
$window\u width2=intval($window\u width);
echo$窗口宽度2;

$window\u width
将为
“1366”

$window\u width2
将为
0


我需要
$window\u width2
整数
,而不是
字符串

$window\u width
不能是1366,因为您混合了PHP和Javascript


PHP在服务器端执行,而JS在用户端执行。这意味着您不能仅在PHP中使用JS中的值。

$window\u width
持有一个
字符串(而不是
window.innerWidth
),该字符串不能转换为
字符串
,因此返回
0

PHP
服务器上执行(在执行
JavaScript
之前),因此无法访问
窗口。在
PHP
上,使用
AJAX
通过
PHP
发送数据


字符串很可能返回0,尽管这取决于字符串最左边的字符。常见的规则适用。

我的规则是1366。
intval-获取变量的整数值
,请检查此处>,并且您的
$window\u width
不包含整数值。您还会发现,您将客户端脚本与服务器端执行混合在一起。例如,
window.innerWidth
将在页面呈现后可用,在此之后,php已经完成了一段时间。因此,该值对于它来说是不可用的,比如ajax,一个不太理想的数据库。intval返回整数值。它不会将字符串转换为integer@OwaisArain-你错了,它会将字符串转换为整数,例如
intval('1')
确实会变成
1
,但是
不是一个数字,然后变成
0
,我想我已经掌握了窍门。非常感谢!