Page 1 of 1

How to write (if and elseif) multi

Posted: Tue Sep 28, 2010 11:50 am
by egturnkey
Hi there,

for every case we have "gooo" as commen in all
how this could be one code using anything like elseif or switch whatever...

Code: Select all

if($refere && (stripos($r, $refere) === false)){ 
echo "x1"
}else{
echo"goooo"
}
And

Code: Select all

if($limited && ($line[hits] >= $limited)){ 
echo "x2"
}else{
echo"goooo"
}
And

Code: Select all

if($pword && (isset($_POST['password']) && $_POST['password'] == $pword) ){
echo"goooo"
}else{
echo"x3"
}
And

Code: Select all

if ($line[capt] == 1 && ($_SESSION["security_code"]) ){
echo"goooo"
}else{
echo"x4"
}

thanks in advance

Re: How to write (if and elseif) multi

Posted: Tue Sep 28, 2010 11:56 am
by Jonah Bron
I think you mean this?

Code: Select all

if ($refere && (stripos($r, $refere) === false)) {
    echo "x1";
} elseif ($limited && ($line['hits'] >= $limited)) {
    echo "x2";
} elseif (!$pword && !isset($_POST['password']) && $_POST['password'] != $pword) {
    echo "x3";
} elseif ($line['capt'] != 1 && !$_SESSION['security_code']) {
    echo "x4";
} else {
    echo "gooo";
}

Re: How to write (if and elseif) multi

Posted: Tue Sep 28, 2010 11:58 am
by twinedev
Given those conditions, I would do it this way (just a personal preference)
Note, I would order the checks from most likely to occur at the top to the least likely to occur at the bottom.

Code: Select all

$strResult = 'goooo';

if ($refere && (stripos($r, $refere) === FALSE)) {
    $strResult = 'x1';
}
elseif ($limited && ($line['hits'] >= $limited)) {
    $strResult = 'x2';
}
elseif (!$pword || !isset($_POST['password']) || $_POST['password'] != $pword) {
    $strResult = "x3";
}
elseif ($line[capt] != 1 || !$_SESSION["security_code"] ) {
    $strResult = 'x4';
}

echo $strResult;