0

trying to get window width on server side

$w = "<script>document.write(screen.width);</script>";
echo $w;  // 1600
echo '<br>' . gettype($w); // string

now I need an integer instead the string
tried a lot of ways from here

for example

$w = intval($w);  
$w = (int) $w; 
settype($w, "integer"); 

echo $w;  // 0
echo '<br>' . gettype($w);  // integer  

so result is allways 0 and integer

how to explain - php sees the variable but cannot handle with it ?
and how can I get 1600-integer

provance
  • 877
  • 6
  • 10
  • 2
    Not at all, because $w is a string and will parse to 0. PHP does not execute JavaScript, the browser does. But, you could run JS via https://www.php.net/manual/de/class.v8js.php which will not give a window width, because it runs on server, not on client. – Markus Zeller May 24 '22 at 09:51
  • 1
    @MarkusZeller - `echo $w` is a server side, isn't it ? – provance May 24 '22 at 09:53
  • 1
    All PHP code is server side. What is echoed will be displayed/run in STDOUT saying browser / terminal. – Markus Zeller May 24 '22 at 09:54
  • 1
    If you want to send data from browser to server you can use AJAX. – Markus Zeller May 24 '22 at 09:55
  • if `echo $w` can be run - what is the problem with `$w = intval($w);` ? What is the difference between the two ? – provance May 24 '22 at 09:58
  • 2
    `$w` is a JS string, containing ``. The `1600` is displayed by the browser by executing the script. Use `var_dump($w)` or view the raw page source and you will see this. – user3783243 May 24 '22 at 10:03
  • @user3783243 - so php can see a js string - and can detect a datatype of it - but cannot convert it ? That is what is strange – provance May 24 '22 at 10:07
  • "_so php can see a js string_" No it can't – brombeer May 24 '22 at 10:09
  • @provance What do you mean "see"? You wrote the JS so PHP has it. – user3783243 May 24 '22 at 10:12
  • @brombeer - executing `echo $w` and `gettype($w)` should means - it see it - isn;t it ? – provance May 24 '22 at 10:12
  • @provance Run `echo htmlspecialchars($w);` hopefully that clarifies it. – user3783243 May 24 '22 at 10:13
  • 2
    No, other users already said so. Please read the linked article [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming). Did you try `var_dump($w)` as suggested? – brombeer May 24 '22 at 10:13
  • @user3783243 - but that is my question - why some works - for example `echo` and `gettype` and some doesn't work – provance May 24 '22 at 10:15
  • 1
    Also already mentioned multiple times. `echo` and `gettype` DO NOT WORK as you think. `echo` prints out the javascript which in turn prints `1600` to the document - on the client side - PHP knows nothing about the client. If `$w` would hold the value `1600` its type would not be `string` but `integer` – brombeer May 24 '22 at 10:17
  • 1
    `echo` sends it to the browser, the browser then renders it. `gettype` casts it as it is. It has been said multiple times already. `` is not an integer, it is a string. A browser will process that to get the width and give an integer. – user3783243 May 24 '22 at 10:17
  • 2
    @user3783243 - I see now, in page source code there is no `1600` but the string ` – provance May 24 '22 at 10:24
  • 2
    Exactly, that's what we have been trying to explain, glad you get it now :-) – ADyson May 24 '22 at 11:05

0 Answers0