I've build my nice page in PHP, but I would like to get a different output order based on the result of a if condition applied at the beginning of the page. IE:
if(condition is true){
block n1
block n2 }
else{
block n2
block n1 }
Can you kindly advise on what's the best practices in this case? I think a flag should solve the problem, but I'm struggling to understand how.
CodePudding user response:
You could prepare you blocks and then exactly what you wrote :
$block1 = "12345";
$block2 = "67890";
$inverted = true;
if ($inverted===true) {
echo $block2.$block1;
} else {
echo $block1.$block2;
}
CodePudding user response:
There are several approaches to this. One would be with if
and else
and if you have many possibilities, then switch case
would be advisable.
$check = 1;
if( $check === 1) {
echo `show 1`;
} else {
echo 'It is not 1';
}
// OR
switch($check) {
case 1:
echo 'Show 1';
break;
case 2:
echo 'Show 2';
break;
case 3:
echo 'Show 3';
break;
default:
echo 'It is not 1,2,3';
}
CodePudding user response:
u can use array just like that
$orders = ['a', 'b'];
if (1 === 1) {
$orders = ['b', 'a'];
}
with this you don't need "else" part
foreach ($orders as $order){
echo $order."<br/>";
}