By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,985 Members | 2,877 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,985 IT Pros & Developers. It's quick & easy.

check string rep's integer

P: n/a
Is there a PHP function to verify that a string validly represents an
integer?
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
*** sinister wrote/escribió (Fri, 21 Nov 2003 15:10:16 GMT):
Is there a PHP function to verify that a string validly represents an
integer?


Check is_int() manual page.

--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #2

P: n/a
"sinister" <si******@nospam.invalid> writes:
Is there a PHP function to verify that a string validly represents an
integer?


See the Variable Functions chapter in the PHP manual:

http://www.php.net/manual/en/ref.variables.php

The answer may not be as simple as you'd initially think, so be
sure to read a function's description and user-contributed notes
before using it. Test whatever code you end up writing to make
sure it gives the expected results for both integers and non-integers.

You could also use a regular expression, but using variable functions
would probably make your intentions more clear.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
Jul 17 '05 #3

P: n/a

"Alvaro G Vicario" <al******************@telecomputeronline.com> wrote in
message news:1p*****************************@40tude.net...
*** sinister wrote/escribió (Fri, 21 Nov 2003 15:10:16 GMT):
Is there a PHP function to verify that a string validly represents an
integer?
Check is_int() manual page.


No; that checks whether a variable is an integer, not whether a string
variable corrected represents an integer.

I'm thinking of
"66" true
"00037" true
"afdfdfd" false
"-00218" true
"- 18" false

--
--
-- Álvaro G. Vicario - Burgos, Spain
--

Jul 17 '05 #4

P: n/a

"Michael Fuhr" <mf***@fuhr.org> wrote in message
news:3f********@omega.dimensional.com...
"sinister" <si******@nospam.invalid> writes:
Is there a PHP function to verify that a string validly represents an
integer?
See the Variable Functions chapter in the PHP manual:

http://www.php.net/manual/en/ref.variables.php


Not sure I understand the connection to variable functions. I would have
thought that I need a function on a string, returning a boolean, and that
either (a) it's already in php, or (b) I'd have to write my own.

I've done (b) in C and C++.
The answer may not be as simple as you'd initially think, so be
sure to read a function's description and user-contributed notes
before using it. Test whatever code you end up writing to make
sure it gives the expected results for both integers and non-integers.

You could also use a regular expression, but using variable functions
would probably make your intentions more clear.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

Jul 17 '05 #5

P: n/a
"sinister" <si******@nospam.invalid> writes:
"Michael Fuhr" <mf***@fuhr.org> wrote in message
news:3f********@omega.dimensional.com...
"sinister" <si******@nospam.invalid> writes:
Is there a PHP function to verify that a string validly represents an
integer?


See the Variable Functions chapter in the PHP manual:

http://www.php.net/manual/en/ref.variables.php


Not sure I understand the connection to variable functions. I would have
thought that I need a function on a string, returning a boolean, and that
either (a) it's already in php, or (b) I'd have to write my own.


Did you look at the list of functions at the above link? If none
of them seem suitable, could you explain why? As I mentioned in
my first followup, the solution isn't as obvious as it might seem
at first, but it's there if you read the documentation and user
comments.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
Jul 17 '05 #6

P: n/a

"Michael Fuhr" <mf***@fuhr.org> wrote in message
news:3f**********@omega.dimensional.com...
"sinister" <si******@nospam.invalid> writes:
"Michael Fuhr" <mf***@fuhr.org> wrote in message
news:3f********@omega.dimensional.com...
"sinister" <si******@nospam.invalid> writes:

> Is there a PHP function to verify that a string validly represents an > integer?

See the Variable Functions chapter in the PHP manual:

http://www.php.net/manual/en/ref.variables.php
Not sure I understand the connection to variable functions. I would have thought that I need a function on a string, returning a boolean, and that either (a) it's already in php, or (b) I'd have to write my own.


Did you look at the list of functions at the above link? If none
of them seem suitable, could you explain why? As I mentioned in


OK, looking at the page again, I see what you mean. Basically, it's (b)
"I'd have to write my own," but the user comments show how to make it short
and sweet.

Thanks.

Best,

S
my first followup, the solution isn't as obvious as it might seem
at first, but it's there if you read the documentation and user
comments.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

Jul 17 '05 #7

P: n/a
*** sinister wrote/escribió (Fri, 21 Nov 2003 20:14:29 GMT):
Check is_int() manual page.


No; that checks whether a variable is an integer, not whether a string
variable corrected represents an integer.


You are right. Probably intval() is a better choice.

--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.