Re: [PHP] checkng if string is a number

2005-11-17 Thread Curt Zirzow
On Thu, Nov 17, 2005 at 02:37:49PM -0500, blackwater dev wrote:
 How can I check to see if a string var holds a number?  Is regex the
 only way to do this?  For example I have code where the var could hold
 10 or ten.  If the string is a number, do one thing, if it isn't
 do something else. is_int doesn't work, intval converts ten to a
 number so that doesn't work

http://php.net/ctype_digit

Curt.
-- 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] checkng if string is a number

2005-11-17 Thread Max Belushkin
  Which version of PHP are you using? On php 4.4.0 (SuSE 10.0 default RPM, not 
custom build, so perhaps I'm missing some extras you're using?) the following 
script works fine:
?php
  if (10==intval(ten)) echo boo\n; else echo no boo\n;
?

  I get no boo. intval doesn't convert my ten to a number. Moreover, the 
output of the following:

?php
  echo intval(ten);
?

  produces an expected (by me) 0.

On Thursday 17 November 2005 20:37, blackwater dev wrote:
 do something else. is_int doesn't work, intval converts ten to a
 number so that doesn't work

 Thanks!

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] checkng if string is a number

2005-11-17 Thread Max Belushkin
  If I misunderstood the intval part of your post, than the [proposed already] 
is_numeric solutions are the way to go. is_numeric will, however, unlike the 
conversion functions, return false if there's anything but a number in the 
string, so, i.e., is_numeric (10a1) will return false, but intval (10a1) 
will give 10.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] checkng if string is a number

2005-11-17 Thread Max Belushkin
  Curt,

  yes, technically, 0 is a number - it's a bit late here, and I believe I
mis-understood the original question, which is why I re-posted in the thread.

  I guess my mind got a bit confused by the combination of is_int doesn't
work, intval returns a number and '10' vs 'ten' example.

  That's probably due to the fact I normally rely on intval() to do the job
for integers, which also helps remove some user mis-types at the end of
number fields, which is useful in situations when
A) you're ok with the user typing 11l when they mean 111
B) you don't want to accept a value of 0

  In principle, that's the only problem with intval - a 0 will evaluate
 as false in a logical statement. Unless one wants to make sure it's a full
 number from start to end, of course...

  Max.

On Thursday 17 November 2005 23:05, you wrote:
 On Thu, Nov 17, 2005 at 08:47:30PM +0100, Max Belushkin wrote:
extras you're using?) the following script works fine:
 
  ?php
if (10==intval(ten)) echo boo\n; else echo no boo\n;
  ?
 
I get no boo. intval doesn't convert my ten to a number.
Moreover, the output of the following:

 Yes it does.

   echo intval('a'); // 0
   echo intval('0'); // 0
   echo intval(0);   // 0
   echo intval(0.1); // 0

   $v = intval('ten');
   var_dump($v); // int(0)

   $v = intval('10');
   var_dump($v); // int(10)

 Curt.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php