-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);
6
  • 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? Commented Apr 19, 2011 at 21:31
  • How can you get 1024 printing $width_s??? You can't mix server-side and client-side code this way. Commented Apr 19, 2011 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?? Commented Apr 19, 2011 at 21:33
  • but i get " $width_s" this value. If you don't believe, try my code :) Commented Apr 20, 2011 at 6:36
  • 1
    This is one of the reasons why I ignored the php tag. (Not the question or the answers, but these comments.) Commented Apr 20, 2011 at 10:20

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.