You cannot mix PHP and JavaScript code this way.
Once the PHP code is executed, its output is sent to the browser, and there is no direct way to "read" the result of client side code (JavaScript) that you have output.
What your code above just does is comparing the string "<script>...</script>"
to 1920
, which will always evaluate to false
, hence reaching code block 3.
If you want to display a div based on the screen size, you can do it in JavaScript alone:
Note: all the JavaScript examples below use jQuery.
<div class="a" style="display: none;"></div>
<div class="b" style="display: none;"></div>
<div class="c" style="display: none;"></div>
<script>
var windowSize = window.outerWidth;
if (windowSize >= 1920) {
$('.a').show();
} else if (windowSize >= 1000){
$('.b').show();
} else {
$('.c').show();
}
</script>
Or better yet, using CSS media queries and no JavaScript at all.
If however you want to pass on this information to your server, you can get JavaScript to call a PHP script on your server, for example:
<script>
$.get('window-size.php?size=' + window.outerWidth);
</script>
This will be a separate PHP call that will occur after the page has loaded.