Adam Adam - 4 months ago 5
PHP Question

PHP function within an IF ELSE

I'm trying to write a PHP IF/ELSE script that calls a function within each IF statement. Although the below works, I don't like having to repeat code when I could call a function.

Any and all advice would be appreciated. Thank you.

PHP - Attempt 1

// Group Outputs
if ($group1 > $group2 && $group1 > $group3 && $group1 > $group4 && $group1) {
$result = '1';
update_usermeta(absint($user_ID),'foo',wp_kses_post('NO'));
update_usermeta(absint($user_ID),'bar',wp_kses_post($result));
} elseif ($group2 > $group1 && $group2 > $group3 && $group2 > $group4 && $group2) {
$result = '2';
update_usermeta(absint($user_ID),'foo',wp_kses_post('NO'));
update_usermeta(absint($user_ID),'bar',wp_kses_post($result));
} elseif ($group3 > $group1 && $group3 > $group2 && $group3 > $group4 && $group3) {
$result = '3';
update_usermeta(absint($user_ID),'foo',wp_kses_post('NO'));
update_usermeta(absint($user_ID),'bar',wp_kses_post($result));
} elseif ($group4 > $group1 && $group4 > $group2 && $group4 > $group3 && $group4) {
$result = '4';
update_usermeta(absint($user_ID),'foo',wp_kses_post('NO'));
update_usermeta(absint($user_ID),'bar',wp_kses_post($result));
} elseif ($group5 > $group1 && $group5 > $group2 && $group5 > $group3 && $group5) {
$result = '5';
update_usermeta(absint($user_ID),'foo',wp_kses_post('NO'));
update_usermeta(absint($user_ID),'bar',wp_kses_post($result));
} elseif ($group6 > $group1 && $group6 > $group2 && $group6 > $group3 && $group6) {
$result = '6';
update_usermeta(absint($user_ID),'foo',wp_kses_post('NO'));
update_usermeta(absint($user_ID),'bar',wp_kses_post($result));
} elseif ($group7 > $group1 && $group7 > $group2 && $group7 > $group3 && $group7) {
$result = '7';
update_usermeta(absint($user_ID),'foo',wp_kses_post('NO'));
update_usermeta(absint($user_ID),'bar',wp_kses_post($result));
} elseif ($group8 > $group1 && $group8 > $group2 && $group8 > $group3 && $group8) {
$result = '8';
update_usermeta(absint($user_ID),'foo',wp_kses_post('NO'));
update_usermeta(absint($user_ID),'bar',wp_kses_post($result));
} else {
echo 'Error';
}


PHP - Attempt 2

// Update Profile
function update($result){
echo $result;
update_usermeta(absint($user_ID),'foo',wp_kses_post('NO'));
update_usermeta(absint($user_ID),'bar',wp_kses_post($result));
}

// Group Outputs
if ($group1 > $group2 && $group1 > $group3 && $group1 > $group4 && $group1) {
update('1');
} elseif ($group2 > $group1 && $group2 > $group3 && $group2 > $group4 && $group2) {
update('2');
} elseif ($group3 > $group1 && $group3 > $group2 && $group3 > $group4 && $group3) {
update('3');
} elseif ($group4 > $group1 && $group4 > $group2 && $group4 > $group3 && $group4) {
update('4');
} elseif ($group5 > $group1 && $group5 > $group2 && $group5 > $group3 && $group5) {
update('5');
} elseif ($group6 > $group1 && $group6 > $group2 && $group6 > $group3 && $group6) {
update('6');
} elseif ($group7 > $group1 && $group7 > $group2 && $group7 > $group3 && $group7) {
update('7');
} elseif ($group8 > $group1 && $group8 > $group2 && $group8 > $group3 && $group8) {
update('8');
} else {
echo 'Error';
}


PHP - Working

// Group Outputs
$result = '';
if ($group1 > $group2 && $group1 > $group3 && $group1 > $group4 && $group1) {
$result = '1';
} elseif ($group2 > $group1 && $group2 > $group3 && $group2 > $group4 && $group2) {
$result = '2';
} elseif ($group3 > $group1 && $group3 > $group2 && $group3 > $group4 && $group3) {
$result = '3';
} elseif ($group4 > $group1 && $group4 > $group2 && $group4 > $group3 && $group4) {
$result = '4';
} elseif ($group5 > $group1 && $group5 > $group2 && $group5 > $group3 && $group5) {
$result = '5';
} elseif ($group6 > $group1 && $group6 > $group2 && $group6 > $group3 && $group6) {
$result = '6';
} elseif ($group7 > $group1 && $group7 > $group2 && $group7 > $group3 && $group7) {
$result = '7';
} elseif ($group8 > $group1 && $group8 > $group2 && $group8 > $group3 && $group8) {
$result = '8';
} else {
echo 'Error';
}

// Update Profile
if (!empty($result)){
update_usermeta(absint($user_ID),'foo',wp_kses_post('NO'));
update_usermeta(absint($user_ID),'bar',wp_kses_post($result));
}

Answer

Try:

$value = "";
if ($group1 > $group2) {
  $value = 'Value 1';
} elseif ($group2 > $group1) {
  $value = 'Value 2'; 
} elseif ($group3 > $group1) {
  $value = 'Value 3';
}

update_usermeta(absint($user_ID),'purchased',wp_kses_post('NO'));
update_usermeta(absint($user_ID),'value',wp_kses_post($value));