How to write (if and elseif) multi

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
egturnkey
Forum Commoner
Posts: 34
Joined: Sun Jul 26, 2009 7:35 pm

How to write (if and elseif) multi

Post 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
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: How to write (if and elseif) multi

Post 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";
}
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: How to write (if and elseif) multi

Post 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;
Post Reply