如何确定在php中使用的screen.width的变量window.width?

I need to know the screen size in jquery or javascript and I need this value is printed on a php variable that will determine whether or not an item is displayed.

var width = $(window).width();  

if (width <= 1024) {
   ///// How can I set a variable here and use it with php?
}

Thanks!

mate, you never get the window width from your php code, php code is executing on your web server, you can only get your window size from javascript which is working on user's browser, you just need find a way sending the width you got from js to your php code, that's it !

sample code :

function myJavascriptFunction() { 
  var javascriptVariable = "John";
  window.location.href = "YOURPHPFILE.php?name=" + javascriptVariable; 
}

I think I understand what you are asking but please tell me if I didn't get it correctly.

An idea that came immediately to mind is where you gather the viewport width and height information as soon as possible and then reload the page but pass the viewport information variables through the URL and receive them in PHP.

Try this code for the suggestion above. Normally, you put your script in an onload function but in this case, you want the dimensions as soon as possible.

<script>
var width = window.innerWidth;
var height = window.innerHeight;
location = location+'?w='+width+'&h='+height;
</script>

The second option is that you can do something complicated with AJAX. The third options is where you can use CSS media queries (as mentioned in the other answer[s]).

If you just want to display elements based on the browser width, use CSS media queries and the display:none; property to hide elements.