Reputation: 2056
Following is my piece of code in which I am assigning my screen width to variable $width
. But when I convert it to integer and echo it, 0
is returned.
$width = '<script> document.write(window.screen.width); </script>';
//if i echo $width it shows 1366 which is width of my screen
$height = '<script> document.write(window.screen.height); </script>';
echo "The height is = $height"."<br>The width is = ".$width;
$a = (int)($width);
echo $a;
I am asking for guidenance to make this conversion perfect.
Upvotes: 1
Views: 1192
Reputation: 3042
You don't need parantheses. Try $a = (int) $width;
or $a = intval($width);
.
Also, there is no sense in the code you are running.
$width = '<script> document.write(window.screen.width); </script>';
This will just set $width
to be the string containing the HTML code (with the JavaScript inside). You can't fetch the return value of the JavaScript code that easily into a PHP variable, mainly because the JS part is executed way after the PHP is done (PHP is handled by the server, the generated HTML (with JS inside) is sent to the browser and the browser takes care of executing JS). To do that, you will need AJAX.
Upvotes: 1
Reputation: 4868
It would be intval but what you are doing isn't possible because you cannot pass client data from the browser through to a php script without $_POST, $_GET, or the similar.
Upvotes: 1
Reputation: 33502
You cannot use javascript to echo out a PHP variable. The javascript isn't executed until the PHP is LONG finsihed doing what it does. The best you can do with javascript is to ajax the data back, but that could only be used in a fresh PHP script - and not the current page.
You certainly can't expect data to come back before the script has finished executing.
Upvotes: 1
Reputation: 190907
You aren't actually getting the screen dimensions at that time. That runs on the client. You will have to make an ajax call to the server to set it.
Upvotes: 5