www.webdeveloper.com
Results 1 to 7 of 7

Thread: if/else statement cleanup. It works, but I think it could be much tidier.

  1. #1
    Join Date
    Aug 2013
    Location
    Dallas, TX
    Posts
    4

    Question if/else statement cleanup. It works, but I think it could be much tidier.

    Ok so I wanted to write some PHP that would display if a store was open or closed during its operation hours. My current code displays this: "The time is: hh:mm am/pm and we are currently open/closed." Which is exactly what I want it to do, but it just seems overly complicated and messy to me. I just started learning PHP this week and am looking for a little insight to clean coding I guess.

    Here is what I have:

    PHP Code:
    <?php 

    $date 
    = new DateTime(date('h:i'));
    $date -> modify('-25200 seconds');
    $day date('w');
    $time =  date('H');
    $hoursTueWedFri = array(1617181920212223);
    $hoursThu = array(001617181920212223);
    $hoursSat = array(16171819202122);
    //time is 7 hrs ahead

    <p>The time is: <?php echo $date->format('g:i a'); ?> and we are currently 

    <?php

        
    if ($day == 0) { 
            echo 
    "closed"
        } 

        if (
    $day == 1) { 
            echo 
    "closed"
        }

        if (
    $time == $hoursTueWedFri) { 
            echo 
    "open"
        }

        elseif (
    $time == $hoursThu) { 
            echo 
    "open"
        } 

        elseif (
    $time == $hoursSat) { 
            echo 
    "open"
        } 

        else { 
            echo 
    "closed"
        }

    //Store Hours:
    //Sunday - CLOSED 
    //Monday - CLOSED 
    //Tuesday 9:00 AM - 6:00 PM = 16 - 23
    //Wednesday 9:00 AM - 6:00 PM = 16 - 23
    //Thursday 9:00 AM - 7:00 PM = 16 - 00 
    //Friday 9:00 AM - 6:00 PM  = 16 - 23
    //Saturday 9:00 AM - 5:00 PM = 16 - 22

    ?>.</p>
    You'll notice that I modified the date for my time zone. This isn't urgent or anything, I just want to try and learn how to clean my code up so I can get better!

    Thoughts?
    Last edited by NogDog; 08-28-2013 at 11:07 PM. Reason: changed CODE tags to PHP tags

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,154
    I won't say this is the "best" way, but it's certainly different, and just one if.
    PHP Code:
    <?php

    date_default_timezone_set
    ('America/New_York');
    $storeHours = array(
        array(), 
    // Sunday
        
    array(),
        
    range(1623),
        
    range(1623),
        array(
    01617181920212223),
        
    range(1623),
        
    range(1622)
    );
    $status 'closed';
    if(
    in_array(date('G'), $storeHours[date('w')])) {
        
    $status 'open';
    }
    echo 
    "We are $status";
    Last edited by NogDog; 08-28-2013 at 11:28 PM. Reason: got rid of else
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Nov 2010
    Posts
    1
    It's a good idea to start using some PHP Template Engine from the very beginning of your learning. It allows you to keep HTML and PHP code in separate files. HTML can look something like that:

    HTML Code:
    <html>
      <body>
         The time is: {Time} and we are currently {Status}
      </body>
    </html>
    And the PHP script like that:

    PHP Code:
    <?php 
      
    require_once("phemplate.class.php");

      
    $tpl = new phemplate("__website_root_path__""remove_nonjs");
      
    $tpl->vars['main'] =  file_get_contents("__filename_of_your_html_template__");

      
    $tpl->set_var('Time'date("h:i"time()));
      
    $tpl->set_var('Status'getStatus());

      echo 
    $tpl->process('out''main'true);
    ?>

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,153
    Neat code NogDog, only thing I would change is the way you allocate the $status, something like this.
    PHP Code:
    $status = (in_array(date('G'), $storeHours[date('w')]))? "open" "closed"
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,154
    I work with some people who hate ternary operators, so I avoid them out of habit now.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,153
    IDK what is to hate about them, maybe they just don't understand them, especially when they are chained together...
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,154
    Quote Originally Posted by \\.\ View Post
    IDK what is to hate about them, maybe they just don't understand them, especially when they are chained together...
    It's purely the readability issue. When scanning through unfamiliar code to find a bug (which we often spend more time doing than actually creating code), it is easier for the eye to see the logic of a properly indented/bracketed if/else block than a one-liner using ternary operators. Sure: they understand the ternary syntax and how to read it, it just slows you down a bit (especially if viewing code in a terminal window or such that does not have syntax-highlighting). Of course, you can spread it out over multiple lines, which helps a bit:
    PHP Code:
    $foo = ($something == $nothing)
         ? 
    getSomeValue($ix)
         : 
    getSomeOtherValue($ix)

    I still sneak in the occasional simple one-liner, though, like:
    PHP Code:
    $foo = isset($_GET['foo']) ? (int) $_GET['foo'] : 0
    (If they can't deal with that, tough.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles