Javascript屏幕宽度来自php

i tried to do like this. i think its worthless

here my code..

<?php
$screen = '<script type="text/javascript">document.write(screen.width);</script>';
$echo $screen;
?>

this javascript code is work without php code..

<script type="text/javascript">
document.write(screen.width);
</script>

but i want to get only width from variable

i tried to like this get screen size from php.. but its not work.. someone can help me for do this one.. i want to variable for screen width.. if have another good method plz give me answer for this one.. thanks.

As mentioned in one of the comment, you need to differentiate client and server.

Though to make things simple, if you want to get screen width on server side, you can do the following :

Index.php

<?php
session_start();
if(!isset($_GET['width']) && !isset($_SESSION['screen.width'])){
?>
<html>
<head>
<script type="text/javascript">
location.replace('http://example.com/?width='+screen.width);
</script>
</head>
<body></body>
</html>
<?php
die();
}

if(isset($_GET['width'])){
    $_SESSION['screen.width'] = (int) $_GET['width'];
}

echo "screen width : ".$_SESSION['screen.width'];

?>