1

Following is my piece of code in which I am assigning my screen width to variable $width. But when I convert it to integer and echo it, 0 is returned.

$width = '<script> document.write(window.screen.width); </script>';
//if i echo $width it shows 1366 which is width of my screen
$height = '<script> document.write(window.screen.height); </script>';
echo "The height is = $height"."<br>The width is = ".$width;
$a = (int)($width);
echo $a;

I am asking for guidenance to make this conversion perfect.

6
  • 4
    You're doing this the wrong way round - JS is executed on the client, not PHP. You're just assigning the script string to $width, which when cast to an integer is 0. Commented Aug 9, 2012 at 12:57
  • 1
    LOL, there is so much "wrong" here. PHP and Javascript execute at different times and places. PHP = Server, Javascript = Client Commented Aug 9, 2012 at 12:58
  • hope you are confused with serverside scripting and clientside scripting. You are trying to typecast string to integer where the string is not number. Commented Aug 9, 2012 at 12:58
  • 1
    @used2could I agree far too much with your first sentence for this to be healthy :P. Commented Aug 9, 2012 at 12:59
  • You could try jquery ajax, it's much easier api.jquery.com/jQuery.ajax Commented Aug 9, 2012 at 13:01

4 Answers 4

5

You aren't actually getting the screen dimensions at that time. That runs on the client. You will have to make an ajax call to the server to set it.

Sign up to request clarification or add additional context in comments.

3 Comments

is there a way to get screen width via PHP?
@softgenic no. PHP is server-side.
You can set a cookie (or use sessions) on the first run, and then PHP will have access to it. So if there's no cookie, run the JS part of the code and refresh the page. When it refreshes, you'll have the screed width in a cookie, the JS part will be skipped and the page will load normally.
1

You cannot use javascript to echo out a PHP variable. The javascript isn't executed until the PHP is LONG finsihed doing what it does. The best you can do with javascript is to ajax the data back, but that could only be used in a fresh PHP script - and not the current page.

You certainly can't expect data to come back before the script has finished executing.

Comments

1

It would be intval but what you are doing isn't possible because you cannot pass client data from the browser through to a php script without $_POST, $_GET, or the similar.

Comments

1

You don't need parantheses. Try $a = (int) $width; or $a = intval($width);.

Also, there is no sense in the code you are running.

$width = '<script> document.write(window.screen.width); </script>';

This will just set $width to be the string containing the HTML code (with the JavaScript inside). You can't fetch the return value of the JavaScript code that easily into a PHP variable, mainly because the JS part is executed way after the PHP is done (PHP is handled by the server, the generated HTML (with JS inside) is sent to the browser and the browser takes care of executing JS). To do that, you will need AJAX.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.