Page 1 of 1
is_int for values of variables?
Posted: Sat Oct 19, 2002 5:37 pm
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... -_-
Posted: Sat Oct 19, 2002 6:27 pm
by sweahe
Try this:
if ($value % 5) {
// what to do if not an integer
} else {
// Things to do if the result is an integer
}
Posted: Sat Oct 19, 2002 6:33 pm
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
Posted: Sat Oct 19, 2002 6:47 pm
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!
Posted: Sat Oct 19, 2002 6:52 pm
by volka
then the
modulus operator sweahe supposed perfectly fits your needs.
Posted: Sat Oct 19, 2002 6:52 pm
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.
Posted: Sat Oct 19, 2002 6:53 pm
by sweahe
Just wrap it in a function then:
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
}
}
Posted: Sat Oct 19, 2002 6:54 pm
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
Posted: Sat Oct 19, 2002 6:57 pm
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

)
Posted: Sat Oct 19, 2002 7:02 pm
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!
if (is_divisable($value, 3)) {
}
Posted: Sat Oct 19, 2002 7:13 pm
by Bennettman
Yeah, with the way I was looking at it at first, is_divisable would have been the most obvious.
Posted: Sat Oct 19, 2002 7:15 pm
by volka
-vs-
I still see no advantage

Posted: Sat Oct 19, 2002 7:19 pm
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?
Posted: Sat Oct 19, 2002 7:21 pm
by volka
probably, but modulus is a basic operator that one should know.
At least you're not getting dumber by knowing it
