-2

What is the problem this code:

$window_width= '<script>screen.availWidth</script>';
$window_height= '<script>screen.availHeight</script>';

any idea?

Sumit Bijvani
  • 7,762
  • 17
  • 46
  • 79
user2688737
  • 53
  • 1
  • 2
  • 6
  • You are declaring two strings with literal string data. There is nothing wrong with this code from a php point of view. – initramfs Oct 10 '13 at 08:54
  • Nothing is wrong with it, if all you want to do is set a couple of variables to string values, but it doesn't really achieve anything.... perhaps you're forgetting the difference between server-side and client-side code – Mark Baker Oct 10 '13 at 08:54
  • 1
    possible duplicate of [Getting the screen resolution using PHP](http://stackoverflow.com/questions/1504459/getting-the-screen-resolution-using-php) – Tomasz Kowalczyk Oct 10 '13 at 08:54
  • 1
    Like @CPUTerminator has said, it's a string, the code inside it, will not be executed like this. – Viezevingertjes Oct 10 '13 at 08:54
  • Javascript runs on the client, PHP on the server. You can't return Javascript values to a PHP script by including code this way. If you need to know the screen size in your PHP script you'll need AJAX to send it. –  Oct 10 '13 at 08:55
  • but doing "echo" does not show any value – user2688737 Oct 10 '13 at 08:55
  • 1
    There is nothing wrong with the code. There is something wrong with your exepctations about what the code should do. What do you think the code should do? What does it actually do? – Oswald Oct 10 '13 at 08:55
  • You can't pass javascript variables to php (because php is serverside and javascript is clientside) this way. – Cyclonecode Oct 10 '13 at 08:56
  • can you show me, please? – user2688737 Oct 10 '13 at 08:57
  • @user2688737 see my answer it will help you – Sumit Bijvani Oct 10 '13 at 08:58
  • PHP works at server side how the hell should it know the size of your browser? The only way is to use AJAX to pass params from JavaScript to your server. – Robert Oct 10 '13 at 09:29

2 Answers2

9

Nothing wrong in your code, but you can't get any values in PHP variable, because PHP is Server side scripting language.

You need JavaScript, not PHP.

var screenWidth = window.screen.width,
var screenHeight = window.screen.height;

You can then send it to the server via Ajax (with an XmlHttpRequest).

Sumit Bijvani
  • 7,762
  • 17
  • 46
  • 79
4

You seem to be misunderstanding the basic concepts of how PHP and HTML/JavaScript works together. PHP is a server-side language and JavaScript is a client-side language.

PHP generates the output first and then it's transferred to the client where it's being executed. You're trying to do it the other way around.

What you need to do is first, generate a page using PHP, have it sent to the client for client-side execution, and from there have JavaScript perform a call that sends the information back to PHP using a GET/POST request.

kba
  • 18,696
  • 5
  • 56
  • 84