Jump to content

If Statement Syntax for Multiple conditions


Skipjackrick

Recommended Posts

Is it possible to do something like I have below?

 

Basically I've got several conditions that need to be true for an if statement.

 

     
<?php
$condition1=1;
$condition2=1;
$condition3=1;
$condition4=0;

if ( $condition1 == 1 && $condition2 == 1  && $condition3 == 1 && $condition4 == 1) 
      {
      //Update kayak man points
      $count_kmpoints = "UPDATE anglers 
            SET kayakman_points = kayakman_points + 1
            WHERE anglerId=$angler";
      $counted_kmpoints = mysql_query($count_kmpoints) or exit(mysql_error());

      } else {
      echo "Sorry, you haven't met the criteria to score any Kayak Man points";
      }
?>

Link to comment
Share on other sites

Yep, that's exactly how you structure it.

 

You can also separate them which can get messy:

 


if($condition1 == 1){

  if($condition2 == 1){
   //etc etc
}

}

 

Or you could place it into a function which would be more manageable but possibly unecessary in this instance:

 


function isValid($conditions){

foreach($conditions as $condition){
  if($condition != 1){
    return false;
  } 
}

  return true;

}

// USAGE:

$c['condition1'] = 1;
$c['condition2'] = 1;
$c['condition3'] = 1;
$c['condition4'] = 0;

if(isValid($c)){
// they are all valid
}

 

Basically, that means you can quickly run through a whole host of conditions without ever having to use anything other than the isValid() line. You could go a step further an add the ability to specify if any conditions can be false, or what certain conditions should return etc. etc.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.