0

Instead of this:

if($today >= $dateOne && $dateOne != '0000-00-00')
    {
    run some code
    }
elseif($today >= $dateTwo && $dateTwo != '0000-00-00')
    {
    run some code
    }
elseif($today >= $dateThree && $dateThree != '0000-00-00')
    {
    run some code
    }
elseif($today >= $dateFour && $dateFour != '0000-00-00')
    {
    run some code
    }
else
    {
    run this code
    }

...which goes up to $dateEleven, is there a better why to do this? I figure there has to be a better way than to have all of these elseif statements.

11
  • How do "some code" differ from another? Commented Jan 17, 2013 at 10:41
  • @phant0m it's always the same. Commented Jan 17, 2013 at 10:42
  • You don't access $dateOne in code block one, $dateTwo in codeblock two etc? Commented Jan 17, 2013 at 10:47
  • @phant0m that is correct. Commented Jan 17, 2013 at 10:49
  • $dateOne etc, are they in the format YYYY-MM-DD? If so, you can't use >= to compare them. Commented Jan 17, 2013 at 10:49

4 Answers 4

1
$dates = Array( 1 => $dateOne, 2 => $dateTwo, ... );
foreach($dates as $key => $date) {
  if($today > $date && $date != '0000-00-00') {
      $date_key = $key;
      break ; // exit loop
  }
}
switch($date_key) {
   case 1:
      // code 1
      break;
   case 2:
      // code 2
      break;
   case 3:
      // code 3
      break;
   case 4:
   case 5:
      // etc.
      break;
}
Sign up to request clarification or add additional context in comments.

4 Comments

This is one slightly closer to my skill level, although I liked the look of it before the first edit ;) Am I right in saying that the switch is used for different types of code? Meaning, if "some code" in my question was not all the same, I would then use this switch?
@AndrewFox yes, this is why i added switch statement
This still requires code duplication though. Your previous version with a default case would have worked better.
@phant0m if you want to use same code remove switch statement, and just do it before break
1

You only have one variable that's actually changing. I don't know know where this is from, but you could put them all into some kind of datastructure, such as an array, and write a loop which does exactly what you want it to do.

Whenever you run into a situation like this, look at what you have and what actually changes. A loop is generally somewhere where you would want to start looking if you're trying to solve anthing like this.

Comments

1

well, if all dates can be '0000-00-00', a loop is unavoidable.

$dates = array($dateOne, $dateTwo ...);
foreach ($dates as $date) {
    if($today >= $date && $dateOne != '0000-00-00') {
        // run some code
        break;
    }
}

However, if these dates are coming from mysql database, you have to use a query to determine if you have to run some code or not.

So, you'd better describe a real life task you're trying to achieve and then get an answer right from the SQL query.

Say, it should be like

SELECT min(date) FROM table WHERE date <> '0000-00-00' AND [other conditions]

and then just compare the returned value, something like this

if ($row && $today >= $row['date']) {

2 Comments

I have already performed the query and now everything is set to variables.
The task is basically meant to raise an alert if $today >= $date. The reason I have != '0000-00-00' is because the database stores that as a default and I don't want that to trigger my alarm...so to speak.
0
function validate($date) {
    return $today >= $date && $date != '0000-00-00';
}

$dates = [$dateOne, $dateTwo, $dateThree, $dateFour];

if(count($filtered = array_filter($dates, 'validate'))) {
    $date = $filtered[0]; // This is the date that triggered the first IF in your code
    // run some code
}
else {
    // run this code
}

4 Comments

I've got to be honest with myself on this answer. This is way above my head and I'd be scared to use it in case I ever had to troubleshoot something later on. I do appreciate the reply though, thank you :)
@AndrewFox That was based on the assumption that all code fragments were different.
Ahh, this is more like it! lol, I can at least understand what I'm reading! I had a feeling that a function would help me out, I just couldn't figure out how to incorporate one.
I hate when people down vote but don't say why! +1 from me either way.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.