Display HTML if two conditions are true or another two are true or third part of conditions are true

StackOverflow https://stackoverflow.com/questions/22973435

  •  30-06-2023
  •  | 
  •  

문제

I have php if statement that should display certain HTML code if two conditions are true or another two are true or third part of conditions are true.

I have several arrays - $Options_arr, $MoreOptions_arr, $Special_arr .

To explain in the easiest possible way I want to do this:

if(!empty($Options_arr[0]) && $Options_arr[0]!="") or

(!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="") or

(!empty($Special_arr[0]) && $Special_arr[0]!="")

{?> some HTML here

All help will be appreciated thank you.

도움이 되었습니까?

해결책 2

BragG, you can use elseif

Like:

if((!empty($Options_arr[0]) && $Options_arr[0]!="") || 
   (!empty($MoreOptions_arr[0]) &&  $MoreOptions_arr[0]!="") || 
   (!empty($Special_arr[0]) &&  $Special_arr[0]!=""))
{
 //  some html or any code
}

I hope that is what you were looking for..

Feel free to ask any question.

다른 팁

empty() already checks for empty string "" so it's shorter:

if(!empty($Options_arr[0]) || !empty($MoreOptions_arr[0]) || !empty($Special_arr[0])) {
    //some HTML here
}

You are just missing some brackets. Also || is more frequently used than OR

if((!empty($Options_arr[0]) && $Options_arr[0]!="") || (!empty($MoreOptions_arr[0]) &&  $MoreOptions_arr[0]!="") || (!empty($Special_arr[0]) &&  $Special_arr[0]!="")){
    echo '<p>hello</p>';
}

You're basically already there...

if (
      (!empty($Options_arr[0]) && $Options_arr[0]!="") 
   || (!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="")
   || (!empty($Special_arr[0]) && $Special_arr[0]!="")

){

...do something

Basically you write an if statement that resolves if any of the sub-statements are true by joining the sub-statements together with ORs

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top