2

PHP

$width = "<script>var windowWidth = screen.width; document.write(windowWidth); </script>";
$height = "<script>var windowHeight = screen.height; document.write(windowHeight); </script>";
if ($width >= 1680) {
    return "OK";
}
else {
    return "NOK";
}

Returning result for $width is 1920, I can see that if I push it's value to the client-side. But it's not numeric nor string. Whatever conditional check I tried, couldn't make the calculation.

I mean;

$width == 1920 // false
intval($width) == 1920 // false (returns 0 as $width value)
(int)$width == 1920 // false (same with float)
$width == "1920" // false

How can I process that $width value for conditional checks properly?

Puwhae
  • 57
  • 7

1 Answers1

2

The problem is due to how PHP and JavaScript work. PHP is server-sided meaning (basically) as soon as someone goes to e.g. https://example.com in their browser the index.php will be executed. When the file has completed executing everything which was echoed or not inside a php tag (<?php ... ?>) will be sent to the browser. The browser will then look at all <script> tags and execute the contained JavaScript.

To conclude it, PHP will be executed on the server and JavaScript will be executed in the browser. You can, however, "talk" with JavaScript to PHP through AJAX requests.

A possible way of doing this is by using jQuery (for AJAX requests) and PHP's sessions to store the width and height of the user's browser.