is_int for values of variables?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

is_int for values of variables?

Post by Bennettman »

Basically, all I need is a way to find out whether ($value / 5) is an integer or not. If it is, it'll do one thing, if not it'll do another. I've had a look around the functions at php.net, but haven't had much luck. Whatever the answer is will probably be simple as hell... sometimes it scares me when people say they're newbies and their example code looks a hell of a lot more complicated than what I've been doing... -_-
sweahe
Forum Commoner
Posts: 52
Joined: Sat May 04, 2002 4:07 am
Location: Växjö, Sweden

Post by sweahe »

Try this:

if ($value % 5) {
// what to do if not an integer
} else {
// Things to do if the result is an integer
}
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

Post by cheatboy00 »

why not just round $value....

if thats not what you want then try this

$a = explode (".", $value)
if (!$a[1]){
// this means its an intiger
} else {
// means it isnt
}

i havent tested that way but it might work
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post by Bennettman »

Well, the idea of it is in a while command. It's to create a line break every 5 times it loops; so dividing the loop counter by 5 would produce an integer every 5 loops, in which case it does the break.

Anyway, cheers sweahe, that works perfectly ^_^ doumo!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

then the modulus operator sweahe supposed perfectly fits your needs.
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

Post by cheatboy00 »

then why not something like this

Code: Select all

$i = 0;
while (something){
   // do stuff
   if ($i = 5) {
      // do line break
      $i = 0;
   }  else {
      $i++;
   }
}
that should work for what you are saying.
sweahe
Forum Commoner
Posts: 52
Joined: Sat May 04, 2002 4:07 am
Location: Växjö, Sweden

Post by sweahe »

Just wrap it in a function then: :wink:

function is_divisable($value, $divisor = 5) {
if ($value % $divisor) {
// what to do if not an integer
$result = false;
} else {
// Things to do if the result is an integer
$result = true;
}

return $result;
}

and call it from within your while statement.
while( whatever... ) {
if (is_divisable($value)) {
// Add your linebreak
}
}
Last edited by sweahe on Sat Oct 19, 2002 6:58 pm, edited 2 times in total.
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post by Bennettman »

cheatboy00 wrote:then why not something like this

Code: Select all

$i = 0;
while (something){
   // do stuff
   if ($i = 5) {
      // do line break
      $i = 0;
   }  else {
      $i++;
   }
}
that should work for what you are saying.
That'd work too... but the other one's quicker ;p
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

function is_divisable($value, $divisor = 5) { ...
despite you're not using the parameter $divisor in this function, what is the advantage of it ?
(overhead is not an advantage ;) )
sweahe
Forum Commoner
Posts: 52
Joined: Sat May 04, 2002 4:07 am
Location: Växjö, Sweden

Post by sweahe »

I know... (I missed it first) you can be without it if you always need the linebreak at the fifth, but it's such an easy overhead if you want the function somewhere else at every 3rd in that loop! :wink:

if (is_divisable($value, 3)) {
}
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post by Bennettman »

Yeah, with the way I was looking at it at first, is_divisable would have been the most obvious.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

if (is_divisable($value, 3)) { 
}
-vs-

Code: Select all

if ($value%3 == 0) { 
}
I still see no advantage ;)
sweahe
Forum Commoner
Posts: 52
Joined: Sat May 04, 2002 4:07 am
Location: Växjö, Sweden

Post by sweahe »

You're absolutely right, that is much shorter... the only difference might be in the readability for a newbie maybe? It's easier to understand is_divisable() maybe?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

probably, but modulus is a basic operator that one should know.
At least you're not getting dumber by knowing it :D
Post Reply