Jump to content

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/282954-documentwrite-value-in-php/
Share on other sites

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 by Ch0cu3r

 

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.

 

 

--

 

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.

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();

}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.