Takács Botond Takács Botond - 6 months ago 129
PHP Question

PHP intval() returns 0

This might seem trivial, but the intval() is returning 0 in the following case:

$window_width = "<script type='text/javascript'>document.write(window.innerWidth);</script>";
$window_width2 = intval($window_width);
echo $window_width2;

$window_width will be "1366".

$window_width2 will be 0.

I need $window_width2 to be integer, not string.


$window_width is holding a string(not window.innerWidth) which can not be converted to string hence 0 is returned!

PHP is executed on sever(Way Before JavaScript is executed) so it is impossible to access window.innerWidth over PHP, use AJAX to emit that data over PHP.

The integer value of var on success, or 0 on failure.

Strings will most likely return 0 although this depends on the leftmost characters of the string. The common rules of integer casting apply.