-3

in my case I want to convert a string value to integer , the value was returned by a JavaScript code, but when i convert in i got, a type int(0) so the values wasn't converted correctly

can anyone help me please, thanks in advance

this is the code

$screenwidth = "<script>document.write(screen.width);</script>";
var_dump($screenwidth);
var_dump((int)$screenwidth);


this what i get as output



string(46)"1200"

int(0)
coding mv
  • 3
  • 3

1 Answers1

0

PHP code is executed on the server, Javascript is sent from the server to the client, where it is then executed. You either want to:

  • echo the script to the page (PHP will not have access to screen width) echo "<script>document.write(screen.width);</script>";
  • make an ajax request to the server with the variable so that PHP can process it (PHP will be in a different context to when page was created) <script>fetch(URL + "/index.php?width=" + screen.width);</script>