Reputation: 226
All things going right until converting a string to an integer. After converting, the value becomes 0. Why?
<script type="text/javascript">
var offset = new Date().getTimezoneOffset(); //get client timezone differance with UTC
offset *= -1; // change sign
offset *= 60; // convert into second
console.log(offset);
</script>
<?php
echo "<br/><br/>";
$a = "<script>document.write(offset)</script>"; //getting value in string
echo $a; // this works and print : 19800
echo "<br/>";
settype($a, "integer");
echo $a; // print 0
?>
Upvotes: 3
Views: 241
Reputation: 43574
You set some HTML (with JavaScript) code in variable $a
. This is not a valid number! So PHP can't convert the not numeric string into a number / integer:
$a = "<script></script>";
var_dump($a); //string(17) "<script></script>"
settype($a, "integer");
var_dump($a); //int(0)
The code is working if you set a numeric value as string (or integer) on the $a
:
$a = "19800";
var_dump($a); //string(5) "19800"
settype($a, "integer");
var_dump($a); //int(19800)
Note: You can't directly assign the result from JavaScript to a PHP variable because the JavaScript code is executed on client side and PHP is executed on server side (before JavaScript). You can use AJAX to send values from JavaScript back to the server to use them on server side too.
Upvotes: 2