Jump to content

Multiple Conditionals


unemployment

Recommended Posts

What is the best way to say...  If more than two of these if's are satisfied, do this...

 

if ((isset($_POST['credentials'])) && ($_POST['credentials'] != $r['credentials']))
{
$details = '';
update_user_actions(2, $details);
}

if ((isset($_POST['specialties'])) && ($_POST['specialties'] != $r['specialties']))
{
$details = '';
update_user_actions(3, $details);
}

if ((isset($_POST['personalweb'])) && ($_POST['personalweb'] != $r['personalweb']))
{
$details = $_POST['personalweb'];
update_user_actions(4, $details);
}

Link to comment
https://forums.phpfreaks.com/topic/234619-multiple-conditionals/
Share on other sites

The quick way would be adding a counter that gets incremented for every satisfied condition. Something like this:

 

<?php
$if_count = 0;

if ((isset($_POST['credentials'])) && ($_POST['credentials'] != $r['credentials']))
{
$details = '';
update_user_actions(2, $details);
        $if_count++;
}

if ((isset($_POST['specialties'])) && ($_POST['specialties'] != $r['specialties']))
{
$details = '';
update_user_actions(3, $details);
        $if_count++;
}

if ((isset($_POST['personalweb'])) && ($_POST['personalweb'] != $r['personalweb']))
{
$details = $_POST['personalweb'];
update_user_actions(4, $details);
        $if_count++;
}

if ($if_count >= 2) {
        //do this 
}
?>

 

If $if_count = 0, no condition is satisfied. If $if_count = 1, 1 condition is satisfied and so on up to 3 (all conditions satisfied).

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.