DonRamos Posted September 22, 2009 Share Posted September 22, 2009 Hello everyone, I have a small script that takes the screen width with screen.width from java. it-s ok to post this value on the screen... but I need to make some operation with this variable. The problem is... I don't know what kind is it? is not integer, string... <?php $width = "<script>document.write(screen.width);</script>"; echo $width; // it shows ok my screen width everytime.... but i cannot do operation with $width... if ($width >= 1280) echo 'OK!'; // it will not write the OK! message.... ?> I tried with (int)$width ... wich will be 0, also tried intval($width) wich also returns 0; tried this small code... wich works ok with any other string but not in my case: $new_string = ereg_replace("[^0-9]", "", $width); echo $new_string; I really don't know how to convert this variable so i can work with it.... Anyone, any ideea ? Thanks. DR Quote Link to comment https://forums.phpfreaks.com/topic/175151-screen-resolution-with-php/ Share on other sites More sharing options...
dennismonsewicz Posted September 22, 2009 Share Posted September 22, 2009 http://us3.php.net/intval Quote Link to comment https://forums.phpfreaks.com/topic/175151-screen-resolution-with-php/#findComment-923149 Share on other sites More sharing options...
mikesta707 Posted September 22, 2009 Share Posted September 22, 2009 you can't do that. Firstly, you are acting as if writing javascript will return the value you wrote. Also, javascript and PHP can't talk to each other. all width has in it is a string with javascript tags and a javasript command, but the PHP can't execute the javascript. casting it as an int wont work either because its a string, and strings always get cast to 0 unless they are integral strings (IE "42", '27', etc.) you can put that value into a post variable, or get variable via javascript, but you won't be able to just access that value Quote Link to comment https://forums.phpfreaks.com/topic/175151-screen-resolution-with-php/#findComment-923154 Share on other sites More sharing options...
PFMaBiSmAd Posted September 22, 2009 Share Posted September 22, 2009 Php code is executed on the web server when the page is requested. The line - $width = "<script>document.write(screen.width);</script>"; does not set $width to the screen.width value. It sets $width to that literal string containing that javascript code that once it is echoed out to the browser and executed in the browser causes the width to be displayed in the browser. To get any information from the browser to the web server it must be passed as part of the HTTP request that is made for a web page. You would need to use AJAX or some other method of making the HTTP request. See the following link for an example of a "poor man's" ajax solution - http://us2.php.net/manual/en/faq.html.php#faq.html.javascript-variable Quote Link to comment https://forums.phpfreaks.com/topic/175151-screen-resolution-with-php/#findComment-923155 Share on other sites More sharing options...
DonRamos Posted September 23, 2009 Author Share Posted September 23, 2009 Thanks everyone for your answers, I understand now.... I was sure of having the value in $width, tried everything for over 10 hrs... I was so tired that couldn't even think anymore now after a good sleep it's more clear. Thanks again. DR. Quote Link to comment https://forums.phpfreaks.com/topic/175151-screen-resolution-with-php/#findComment-923323 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.