/    Sign up×
Community /Pin to ProfileBookmark

my function needs refining

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

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@pyroDec 16.2003 — No, you just need to set it as a global, as well:

[code=php]function increment() {
global $num, $numofimgs;
if ($num > $numofimgs) {
$num = 1;
}
else {
$num ++;
}
}[/code]
Copy linkTweet thisAlerts:
@olafDec 16.2003 — Hallo,

maybe this works beter for you:

[code=php]
for ($num=1; $num < numofimgs; $num++) {
echo "the image";
}
[/code]
Copy linkTweet thisAlerts:
@jasnjohnauthorDec 16.2003 — Thanks guys,

I just added the variable to the global list, and it's working fine.

Silly me!
Copy linkTweet thisAlerts:
@pyroDec 16.2003 — No problem... :p
×

Success!

Help @jasnjohn spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...