Click to See Complete Forum and Search --> : my function needs refining


jasnjohn
12-15-2003, 08:35 PM
I've a simple function as below:

function increment() {
global $num;
$num ++;
}

It works fine.
It's called upon to increment a variable $num.
I have a similar one for decrement too.

I'm using the count variable as the source for an image number (eg; $num.jpg), and that too works fine.
However, I want the function to limit the count to the number of images stored in that folder.
I've an array which I've performed sizeof(array) and assigned it to a variable called $numofimgs.
This too works fine.

SO...

my problem is I can't seem to integrate an IF statement into the function that works. I've tried various methods and the function won't work at all.
example of what I've tried;

function increment() {
global $num;
if ($num > $numofimgs) {
$num = 1;
} else {
$num ++;
}
}

My only thought is that the variable $numofimgs maybe needs to be declared as

int $numofimgs

I'm at work now and can't try it out, but does that sound feasible or is my syntax above screwed up?

Hope someone can help.
Thanks,
John

pyro
12-15-2003, 09:07 PM
No, you just need to set it as a global, as well:

function increment() {
global $num, $numofimgs;
if ($num > $numofimgs) {
$num = 1;
}
else {
$num ++;
}
}

olaf
12-16-2003, 02:38 AM
Hallo,

maybe this works beter for you:


for ($num=1; $num < numofimgs; $num++) {
echo "the image";
}

jasnjohn
12-16-2003, 09:20 AM
Thanks guys,
I just added the variable to the global list, and it's working fine.
Silly me!

pyro
12-16-2003, 10:55 AM
No problem... :p