0

i'm trying to add some html code to my php only if my window width is greater than 1024px. this is what i got so far...

$ScreenWidth = "<script type='text/javascript'>document.write(window.innerWidth);</script>";
if ( $ScreenWidth > 1024 ) { 
    echo 'Desktop';
} else {
    echo 'Mobile';
}

but the problem is that $ScreenWidth is not returning a number, echo $ScreenWidth will return :

<script type="text/javascript">document.write(window.innerWidth);</script>2003

so i cannot use $ScreenWidth if in PHP if condition.

can you guys give me a hint on how to fix this ? or i'll appreciate it if you could suggest a better way to write a PHP condition based on browser window with.

P.S: i do can hide or display none the content using css but as this html codes are loading images and videos i want to able to completely prevent loading them on mobile pages instead of loading and then hiding them using css.

ErfanMHD
  • 1
  • 1
  • 2
  • 3
    Does this answer your question? [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) (PHP is server side and JS is client side) – M. Eriksson Sep 11 '20 at 23:53
  • This is not really possible in PHP alone (Say for the deprecated experimental `Viewport-Width` header), what you can do is make the extra HTML a different endpoint and then load that into the document using JS - Though a better technique is to add a "skeleton" in JS and then load that using endpoint data. – Elias Schablowski Sep 11 '20 at 23:54

1 Answers1

1

You can't use a result of any JavaScript-function in PHP because JS is clientside and PHP is serverside. The script will be executed on the client and the server won't get any feedback from it (except you use tchniques like Ajax).You had to do this all in JS on the client.

Sascha
  • 4,576
  • 3
  • 13
  • 34