SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,727
    Mentioned
    43 Post(s)
    Tagged
    0 Thread(s)

    There MUST Be a More Elegant Way of Doing This... Odd and Even Test

    Okay so going...

    PHP Code:
            if($i 1)
                {
                
    $output[] = '</tr>';
                } 
    ...is fine. The trouble starts when I go...

    PHP Code:
            if($i 1)
                {}
            else
                {
                
    $output[] = '<tr>';
                } 
    ...I mean it works and all but it's not very graceful... even by my standards.

    I tried...


    PHP Code:
    if($i !& 1
    ...and even...

    PHP Code:
    if!($i 1

    ...but both of those produce syntax errors.

    What's the correct way to do this??

  2. #2
    SitePoint Enthusiast GhostGambler's Avatar
    Join Date
    Apr 2007
    Location
    Germany, NRW
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if ($i &#37; 2 == 0) {

    } elseif ($i != 0) {


    ? oO
    maybe this one is possible
    PHP Code:
    if (($i 1) == 0) { 
    dunno *shrug*

  3. #3
    SitePoint Guru
    Join Date
    Nov 2002
    Posts
    841
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I usually do

    PHP Code:
    if ($i == 0) {} // even parity
    if ($i != 0) {} // odd parity 
    But I think your condition would work with
    PHP Code:
    if ($i 1) {} // odd parity
    if (!($i 1)) {} // even parity 

  4. #4
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,727
    Mentioned
    43 Post(s)
    Tagged
    0 Thread(s)
    Outstanding.

    Many thanks.

  5. #5
    SitePoint Addict SwordsmanX's Avatar
    Join Date
    Sep 2005
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I find top elegance for one liners in this:

    PHP Code:
    ($i &#37; 2==1) ? odd_number()  : even_number(); 

  6. #6
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,727
    Mentioned
    43 Post(s)
    Tagged
    0 Thread(s)
    Hey. Nice.


Bookmarks

Posting Permissions

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