无法在PHP中将字符串转换为整数 [英] Unable to convert string to integer in PHP
问题描述
尽管我使用(int)
和 intval()
函数进行了类型转换所做的所有努力。我仍然无法将字符串转换为整数。
每当我使用这些函数时,字符串转换为0
这是我的代码片段:
inspite of the all the efforts that i gave on typecasting using (int)
and intval()
function. I am still not able to convert string to integer.
Whenever I use these function the string gets converted to 0
Here's my code snippet :
$resolution = "<script type='text/javascript'>
document.write('#'+screen.width+'#');</script >";
$screen=str_replace("#","",$resolution);
echo $wid = (int)$screen;
echo $s = 98 * $wid;
类型转换的输出为0.
我甚至试图打印使用 var_dump
的数据,但它也显示为 int(0)
The output of the typecast is 0.
I even tried to print the data using var_dump
but it also shows as int(0)
推荐答案
您的逻辑存在缺陷。
您正在混合服务器和客户端代码。
Your logic is flawed.
You are mixing server and client code.
服务器生成代码(php),然后将结果传递给客户端。
客户然后接收生成的结果并解析javascript。
Server generates code (php) and then passes the results to the client. Client then receives the generated results and parses the 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;
?>
这篇关于无法在PHP中将字符串转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!