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 8 of 8

Thread: odd even number

  1. #1
    New Coder
    Join Date
    Feb 2003
    Posts
    63
    Thanks
    0
    Thanked 0 Times in 0 Posts

    odd even number

    if I have a for loop :
    for ($i=0; $i<count($replyDetails); $i++){
    ...
    bgcolor = 123 (OR 456)...
    ...
    }
    and will show bgcolor = 123 if $i is an even number or $i=0
    and will show bgcolor = 456 if $i is an odd number

    how can i implement that?

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    PHP Code:

    for ($i=0$i<count($replyDetails); $i++){

    if(
    $i == 0)
        echo 
    $i " is even";
    else
        echo 
    $i "is odd";

    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Depends on the time of year
    Posts
    478
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just to explain what Spookster did, we have a "%" which is a modulos. This means it takes the remainder of the division of two numbers (eg. Remainder of 10%2 is 0 from 10/2 = 5.0 -> which means no remainder).

    So

    if (($i % 2) == 0)

    means for all numbers divisible by 2 (ie. every second number).

    Duniyadnd

  • #4
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Q. Is it better to do it using mod or like this?
    PHP Code:
    if ($xyz == 1){
     
    $bgColor "red";
     
    $xyz 0;
    }else{
     
    $bgColor "green";
     
    $xyz 1;

    Or is the diff non-existant?
    Omnis mico antequam dominus Spookster!

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Depends on the time of year
    Posts
    478
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    for ($i=0$i<count($replyDetails); $i++){

    if(
    $i == 0)
        echo 
    $i " is even";
    else
        echo 
    $i "is odd";

    For loop O(n)
    if O(1)
    echo O(1)
    else O(1)
    echo O(1)

    Maximum time is O(n)


    PHP Code:

    for ($i 0$i<count($replyDetails); $i++)
    {
    if (
    $xyz == 1){
     
    $bgColor "red";
     
    $xyz 0;
    }
    else
    {
     
    $bgColor "green";
     
    $xyz 1;
    }

    for loop O(n)
    if O(1)
    $bgcolor = "red" O(1)
    $xyz = 0 O(1)
    else O(1)
    $bgcolor = "red" O(1)
    $xyz = 0 O(1)

    total O(n)

    In terms of big Oh time, they are equivalent, however, you have more variables in the second one that are changed, so if you are looking at it minutely, that is less efficient, but hardly noticable.

    At least that's my understanding of it.

    Duniyadnd

  • #6
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Ok, thanks. I'm sure any gain in speed and preformance is good no matter how minute.
    Omnis mico antequam dominus Spookster!

  • #7
    Registered User
    Join Date
    Feb 2003
    Location
    Berkshire, UK
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Source: Golden rules for optimising your pages

    Prefer '.....' to "....." because the server side PHP engine parses everything it finds between the " " while it doesn't control what is written between the two single quotes

    By the way: <?php echo 'This is my var:',$var,'!!'; ?> should be some milliseconds faster than <?php echo 'This is my var:'.$var.'!!'; ?> (concatenated by a dot instead of the comma) and faster than <?php print 'This is my var:'.$var.'!!'; ?>
    every little helps <g>

  • #8
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Okey dokey.
    Omnis mico antequam dominus Spookster!


  •  

    Posting Permissions

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