2

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;

The output of the typecast is 0.
I even tried to print the data using var_dump but it also shows as int(0)

MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
Jigar Tank
  • 1,654
  • 1
  • 14
  • 24
  • 2
    There isn't even a single integer in your entire input string, so what would you expect it to parse? – Another Code Mar 15 '12 at 07:03
  • there is indeed a string : "1280" i have echoed it and tested it. It takes the current screen resolution and gives me. I tried it with var_dump and it gave me string : "1280" i.e my screen width. – Jigar Tank Mar 15 '12 at 07:05
  • 1
    No there isn't, PHP doesn't execute javascript. THe literal string you are casting is ``, there are no numbers... – Wesley Murch Mar 15 '12 at 07:06
  • Umm.. no there isn't. There would be, when screen.width is executed, but that happens on the client side AFTER the php is done and the result is sent over to the client. Where exactly did you echo it? Provide a runnable snippet that I could just copy-paste in my php interpreter that would print `1280` or anything similar. – Rohan Prabhu Mar 15 '12 at 07:07
  • @JigarTank It doesn't. What's happening is that you're sending your literal Javascript code to the browser, which executes it upon receiving it, but it's already beyond PHP at that point. Your debugging environment is fooling you: [see what's actually happening](http://codepad.viper-7.com/46za0L) with a `text/plain` view. – Another Code Mar 15 '12 at 07:08

1 Answers1

5

Your logic is flawed.
You are mixing server and client code.

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;
?>
Frankie
  • 24,627
  • 10
  • 79
  • 121