This question already has an answer here:
I'm interested in using a php if statement to include or exclude code based on the user's browser size. For example, a slider will only load if the user's browser size is greater than 768 x 900px.
I know how to hide an object using the css viewport, but I imagine the page speed would increase if I could simply not load the code at all, rather than hiding it.
1) Is PHP capable of detecting a user's browser size?
2) Is an if statement a feasible way of achieving this?
3) Am I correct in assuming that not loading the code altogether is more efficient than just hiding the output via css?
4) I haven't started learning javascript yet, would you recommend that as the best way to achieve this?
Any help is really appreciated!
</div>
PHP is not capable of detecting the user's browser size. The PHP is executed before there even is a user, as it's on the server.
...
If you have tons of code, then yes, it might be more efficient, but a single slider should not make such a big difference. I would recommend just loading it, and hiding via CSS.
As i mentioned in the above bullet point, CSS is going to be more efficient than JavaScript. Just use an @media
query:
@media (min-width: 768px) and (min-height: 900px) {}
If you want to detect the screen size of your client, PHP can't help you, javascript and CSS can.
If you are concern about speed, javascript and CSS can't help you (even if it is minified), PHP can.
My suggestion is, when the client visits your website for the first time, detect their screen size using CSS and after that, save it in a database and set a cookie/session that corresponds with it.
The problem with this workaround is when the client change their screen size before the cookie expires or the session ends.