preetham Posted October 14, 2013 Share Posted October 14, 2013 Hi, I have to detect the Browser size in PHP. What is the best way? ---- What i have currently done is, $myWidth = '<script> document.write(window.innerWidth); </script>'; When i echo $myWidth , i get width, But i'm unable to do any function on that. ( ex, if , strcmp ) Also if do strlen($myWidth) i get 72. But echo gives me value "1366" , that lenght can't be 72. I am quite struck up here. Any help appreciated Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 14, 2013 Share Posted October 14, 2013 (edited) The following $myWidth = '<script> document.write(window.innerWidth); </script>'; Does not execute the javascript. PHP does not know that is javascript within the quotes. It will see it as bunch of characters that make up a string. That string is stored within the $myWidth variable. Also if do strlen($myWidth) i get 72. Because strlen() counts how many characters are stored within the $myWidth variable. But echo gives me value "1366" Echo does not give you that value. Echo prints what ever is in the variable to the browser (right click > view source and to see PHP's output). So echo $myWIdth; will output the following <script> document.write(window.innerWidth); </script> The browser will then execute the JavaScript code code and write whatever the browsers width is to the HTML document, and so you see 1366 displayed. What you see here is PHP and javascript are executed at different times., PHP on the sever and JavaScript in the browser. You cannot capture the output of the JavaScript within PHP and visa-versa . What you're trying to do is not possible. Edited October 14, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted October 14, 2013 Share Posted October 14, 2013 What are you trying to accomplish? What do you need the browser window width for? Perhaps we can direct you down a different path. Quote Link to comment Share on other sites More sharing options...
preetham Posted October 14, 2013 Author Share Posted October 14, 2013 What you see here is PHP and javascript are executed at different times., PHP on the sever and JavaScript in the browser. You cannot capture the output of the JavaScript within PHP and visa-versa . What you're trying to do is not possible. Thanks.. I am still learning. I thought Logically in regular programming way. Thanks you clarifying . What are you trying to accomplish? What do you need the browser window width for? Perhaps we can direct you down a different path. I am trying to check what is the width of the browser. Depending of which i have some action to be done. -- Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted October 14, 2013 Share Posted October 14, 2013 I am trying to check what is the width of the browser. Depending of which i have some action to be done. And what would that action be? Perhaps there is a JavaScript solution. Or maybe you could use CSS media queries. It all depends on what you're looking to do. Quote Link to comment Share on other sites More sharing options...
preetham Posted October 14, 2013 Author Share Posted October 14, 2013 And what would that action be? Perhaps there is a JavaScript solution. Or maybe you could use CSS media queries. It all depends on what you're looking to do. Hi. I want to call a PHP function (Defined in a Plugin) I am using this on wordpress by the way. Here is what i want to do. if($width>900) { plugin_function(); } Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted October 14, 2013 Share Posted October 14, 2013 Have you considered using AJAX? Perhaps something here will help: https://www.google.com/search?q=call+php+function+with+ajax Quote Link to comment 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.