-9

how convert from string to int?

    If i print $width_s, get: 1024 
If i print $width, get 0.

How convert string to integer(number) ?

$width_s = '<script language="javascript">document.write(screen.width)</script>';
$width = (int)$width_s;

var_dump($width);
lolalola
  • 3,773
  • 20
  • 60
  • 96
  • 2
    Are you trying to just return the screen width? Right now you're trying to cast a string that's full of text into an integer in the example you've given... what are you expecting the integer output to be? – DaOgre Apr 19 '11 at 21:31
  • How can you get 1024 printing $width_s??? You can't mix server-side and client-side code this way. – gd1 Apr 19 '11 at 21:32
  • 1
    `(int)"anything that doesn't start with a number"` always evaluates to `0`. What are you actually trying to accomplish? Getting the `screen.width`?? – Kevin Peno Apr 19 '11 at 21:33
  • but i get " $width_s" this value. If you don't believe, try my code :) – lolalola Apr 20 '11 at 06:36
  • 1
    This is one of the reasons why I ignored the `php` tag. (Not the question or the answers, but these comments.) – BoltClock Apr 20 '11 at 10:20
  • Possible duplicate of [How do I convert a string to a number in PHP?](http://stackoverflow.com/questions/8529656/how-do-i-convert-a-string-to-a-number-in-php) – fedorqui Apr 01 '16 at 11:32

3 Answers3

11

PHP is executed on the server side, while javascript is executed on the client side; if you want a value from javascript to be sent to PHP, you will need to write up some kind of script to pass the data, via AJAX, GET, etc.

edit: database != data

Dereleased
  • 9,939
  • 3
  • 35
  • 51
2

You can use the php function intval($string). Learn about it here.

user123
  • 51
  • 5
0

see:

http://www.php.net/intval

The problem with your code is that its a string with characters that aren't numbers, so casting to int isn't possible. Intval returns only the numeric part of a given string.

jcarlosn
  • 398
  • 1
  • 4