...

# Simple Count Function

jream
08-07-2007, 08:33 AM
<?php

\$count = 1;

function colors(){
if (\$count % 2 == 0) echo "blue";
else echo "red";
}
?>

<div>
Test Section 1 <?php \$count++; echo \$colors; ?>
</div>

<div>
Test Section 2 <?php \$count++; ?>
</div>

Not sure why it's not working :P
Im trying to make every other div a different color, once I fugre out how to get the echo right it will be easy!

matak
08-07-2007, 08:38 AM
Try using ++\$count instead of \$count++

If you want to call colors() function, do it like this

colors(), and not echo \$colors

Oh and you need to somehow pass that count variable into function. So do it like this

function colors(\$value) {
//code
}

and call it with

colors(\$count);

jream
08-07-2007, 08:46 AM
Thanks for the quick reply, I was trying to edit the OP but it wouldnt go through, so I changed to what ya said, hehe I call the function wrong but Im not sure why the if statement can't determine an even/odd number:

Out of curiousity, what does ++\$count do rather than \$count++? I don't think a variable can have a + in it, but if so I could see that screwing things up.

<?php

\$count = 1;

function colors(){
if (\$count % 2 == 0) echo "blue";
else echo "red";
}
?>

<div>
Test Section <?php echo ++\$count; colors(); ?>
</div>

<div>
Test Section <?php echo ++\$count; colors(); ?>
</div>

<div>
Test Section <?php echo ++\$count; colors(); ?>
</div>

jream
08-07-2007, 08:47 AM
It keeps echoing blue, so thats an improper way of evaluating a even number?

*Sorry the edit my posts won't work so I have to post again

GJay
08-07-2007, 08:51 AM
you missed the 2nd bit of matak's post- you need to pass the value to the function, otherwise \$count won't 'exist' inside of it:

<?php
\$count = 1;

function colors(\$count){
if (\$count % 2 == 0) echo "blue";
else echo "red";
}
?>

<div>
Test Section <?php echo ++\$count; colors(\$count); ?>
</div>

<div>
Test Section <?php echo ++\$count; colors(\$count); ?>
</div>

<div>
Test Section <?php echo ++\$count; colors(\$count); ?>
</div>

matak
08-07-2007, 08:52 AM
I edited my answer, on few other things you need to change. Difference between ++\$var and \$var++ is best shown in this example

\$count1 = 1;
echo ++\$count; //echoes 2

\$count2 = 1;
echo \$count2++; //echoes 1

You forgot to pass variable \$count into your function...

matak
08-07-2007, 08:58 AM
It keeps echoing blue, so thats an improper way of evaluating a even number?

Just type into google (http://www.google.com/search?q=php+even+odd+numbers&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a) you can find many different ways on few first pages

firepages
08-07-2007, 09:23 AM
Difference between ++\$var and \$var++ is best shown in this example

however

<?
\$count = 1;
\$count++;
echo \$count;
?>
will echo '2' , just as ++\$count would but then do this..
<?
\$count = 1;
echo \$count++; //echoes 1
echo \$count; //echoes 2
?>

... and see that the incrementation is happening, but after the echo is called.

bug or feature ? not sure.
internally ++\$var is faster since \$var++ is converted to ++\$var before evaluation ... but obviously still after a call to a language construct like echo()

jream
08-07-2007, 10:28 AM
Oh thank you guys so much. It works like a beauty :)
Sorry for the delayed reply, I almost took a nap lol
I do skim google and lookup things for this, but sometimes I want to figure it out so bad!!

I thought something had to go into the function colors() parentheses, but I put the entire if/else statement in it and became confused so reverted back.

I'm very rusty on my php, hehe :)

I was going to ask the simple question, Why must count be in the function but you wrote it well and simple GJay: "..you need to pass the value to the function, otherwise \$count won't 'exist' inside of it"

Thank you both for your help, this makes a lot of sense. I like to know what is actually happening and why.

Ill see if I can run some tests on the ++\$add and \$add++, thats pretty weird to me, best learned by practice.

Cheers! :)

matak
08-07-2007, 10:42 AM
bug or feature ? not sure.

it's not bug, and is mostly used in for loops, i never had to explicitly use it, but i saw few scripts that behave totaly different if you just change the increment type