简体   繁体   中英

how to put multiple grouped && , || conditions using if else

I want to check conditions with && ||groupping it means if one group stisfy then execute the first condition otherwise second and so on so i'll do that with the help of (if else) kindly give me an example

Description:-i have a select box with multiple options so diffrent diffrent div will remain open if the selected option value exists in the session.

<div id="bizseg" class="col-sm-4" <?php    if(isset($_SESSION['constitution'])){  if($_SESSION['constitution']=="Individual" && $_SESSION['myprof']=="Self Employed Businessman") {?> style="display:block;" <?php  }else { ?> style="display:none;" <?php }} ?>  style="display:none;">

Shrbham please read what you posted why 2 if statements??? if it is all tests for true just && them here is the code and formated so it can be read but in HTML output will be 1 line

<div id="bizseg" class="col-sm-4" <?php    
    if(
        isset($_SESSION['constitution']) &&
        $_SESSION['constitution'] == $variable &&
        $_SESSION['myprof'] == $variable2 ) {
?> style="display:block;" <?php 
}else { 
?> style="display:none;" <?php } ?>

If's are quite simple true or false

$var1 = "test";
$var2 = "test";
$var3 = "test2";

//so 
$result = $var1 == $var2; // true
$result = $var2 == $var3; // false
//(false)            (true)           // false and anything == false
if($var2 == $var3 && $var1 == $var2); // false
// (false)           (true)           // true or anything == true 
if($var2 == $var3 || $var1 == $var2); // true

if( ($var2 == $var3 || $var1 == $var2) && $var1 == $val3 ) // false
//  ((false)        || (true))         && (false) ==
//  (true)                             && (false) 
//following rules above and false = false;

As "@Lightness Races in Orbit" has said in comments this sort of procedural code is not liked

you can do it as

<?php    
    if(
        isset($_SESSION['constitution']) &&
        $_SESSION['constitution'] == $variable &&
        $_SESSION['myprof'] == $variable2 ) 
    {
        $result = 'style="display:block;"'; 
    }else {
        $result = 'style="display:none;"';
    }
?>
<div id="bizseg" class="col-sm-4" <?php echo $result ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM