Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Sep 2006
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple Count Function

    PHP Code:
    <?php

    $count 
    1;

    function 
    colors(){
    if (
    $count == 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!

  • #2
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    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

    PHP Code:
    function colors($value) {
    //code

    and call it with

    PHP Code:
    colors($count); 
    Last edited by matak; 08-07-2007 at 07:45 AM.

  • #3
    New Coder
    Join Date
    Sep 2006
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 Code:
    <?php

    $count 
    1;

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

    <div>
    Test Section <?php echo ++$countcolors(); ?>
    </div>

    <div>
    Test Section <?php echo ++$countcolors(); ?>
    </div>

    <div>
    Test Section <?php echo ++$countcolors(); ?>
    </div>

  • #4
    New Coder
    Join Date
    Sep 2006
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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 Code:
    <?php
    $count 
    1

    function 
    colors($count){ 
    if (
    $count == 0) echo "blue"
    else echo 
    "red"

    ?>  

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

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

    <div> 
    Test Section <?php echo ++$countcolors($count); ?> 
    </div>
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #6
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    I edited my answer, on few other things you need to change. Difference between ++$var and $var++ is best shown in this example

    PHP Code:
    $count1 1;
    echo ++
    $count//echoes 2

    $count2 1;
    echo 
    $count2++; //echoes 1 
    You forgot to pass variable $count into your function...

  • #7
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by jream View Post
    It keeps echoing blue, so thats an improper way of evaluating a even number?
    Just type into google you can find many different ways on few first pages

  • #8
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,095
    Thanks
    11
    Thanked 101 Times in 99 Posts
    Quote Originally Posted by matak View Post
    Difference between ++$var and $var++ is best shown in this example
    however
    PHP Code:
    <?
    $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()
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #9
    New Coder
    Join Date
    Sep 2006
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #10
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by firepages View Post
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •