Jig*_*ank 2 javascript php string int
尽管我在使用(int)和intval()功能进行类型转换时所做的所有努力.我仍然无法将字符串转换为整数.
每当我使用这些函数时,字符串都会转换为0
这是我的代码片段:
$resolution = "<script type='text/javascript'>
document.write('#'+screen.width+'#');</script >";
$screen=str_replace("#","",$resolution);
echo $wid = (int)$screen;
echo $s = 98 * $wid;
Run Code Online (Sandbox Code Playgroud)
类型转换的输出是0.
我甚至尝试使用打印数据,var_dump但它也显示为int(0)
你的逻辑是有缺陷的.
您正在混合服务器和客户端代码.
服务器生成代码(php),然后将结果传递给客户端.客户端然后接收生成的结果并解析javascript.
<?php
// this is server code, resolution will be just a string to the server
$resolution = "<script type='text/javascript'> document.write('#'+screen.width+'#');</script>";
// you are now removing the # from the above string but keeping it all intact
$screen=str_replace("#","",$resolution);
// converting it to int returns a stupid value (zero?)
echo $wid = (int)$screen;
echo $s = 98 * $wid;
?>
Run Code Online (Sandbox Code Playgroud)