-5


my function is not worked.

    $displayon = "display:none !important";
    $displayoff = "padding:0";

    $displayres = "responsive";
    $displayres1 = "responsive1";

   function displays($check)
   {
    if ($display1 OR $display2 OR $display3 OR $display4 == $displayon){
       echo $displayres1;
       return true;
    }
    else  {
        echo $displayres;
    }

   }


and

<div class="<?php if (displays($check) == true){echo $check;};?>"style="<?php echo $display1;?>">
<div class="<?php if (displays($check) == true){echo $check;};?>"style="<?php echo $display2;?>">
<div class="<?php if (displays($check) == true){echo $check;};?>"style="<?php echo $display3;?>">
<div class="<?php if (displays($check) == true){echo $check;};?>"style="<?php echo $display4;?>">


But not worked.
I want if "$display1 or $display2 or ..." was equal to "$displayon" , Use responsive1. Otherwise, use responsive.
I have a photo gallery of 4 columns.
In the module, you can disable each column.
I want the responsive to become 3 columns if a column is disabled.
And if two columns are disabled, responsive becomes 2 columns.
Each display (display1,2,3,4) represents a column.

Hadi
  • 17
  • 3
  • WOW! Scope issues and non existing variables and invalid IF syntax. Not sure where to start, or whether its even worth starting – RiggsFolly Aug 16 '18 at 22:01
  • Add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly Aug 16 '18 at 22:06
  • 1
    What is `$checks` and why are you passing it to your function as a parameter BUT NOT USING IT in the function – RiggsFolly Aug 16 '18 at 22:08

1 Answers1

1

If I understand correctly, the function needs to do this:

function displays($check) {
    if ($check == "display:none !important") {
        return 'responsive1';
    } else {
        return 'responsive';
    }
}

And be used like this:

<div class="<?php echo displays($display1); ?>"style="<?php echo $display1;?>">
<div class="<?php echo displays($display2); ?>"style="<?php echo $display2;?>">
<div class="<?php echo displays($display3); ?>"style="<?php echo $display3;?>">
<div class="<?php echo displays($display4); ?>"style="<?php echo $display4;?>">

You pass the style string as the argument to the displays function, and return one class or the other depending on whether or not it matches the specific string you're looking for.


Some important things to note:

  • you can't use a variable in a function if it's defined outside the function. (See variable scope)
  • You can't compare multiple things to one thing like this

    if ($display1 OR $display2 OR $display3 OR $display4 == $displayon){
    

    Instead you'll need to do them one at a time like

    if ($display1 == $displayon || $display2 == $displayon ... etc. 
    

    Or put them in an array and use in_array. But I don't think that's what you were going for here anyway.

  • When you define a function like function displays($check), the $check is a parameter, and when the function is executed it will take the value of whatever you put there when you call the function. For example if we say displays('hi'); then $check will have the string 'hi'. And if we say $someVar = 42; and call displays($someVar); then $check will have the value 42.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • Wow thats a leap in the dark :) I hope you are right :) :) – RiggsFolly Aug 16 '18 at 22:15
  • 1
    @RiggsFolly I thought the question was kind of like one of those pictures that if you stare at it long enough the hidden image comes into focus. Maybe just wishful thinking – Don't Panic Aug 16 '18 at 22:17
  • 2
    I can only see a Dolphin :) – RiggsFolly Aug 16 '18 at 22:17
  • I have a photo gallery of 4 columns. In the module, you can disable each column. I want the responsive to become 3 columns if a column is disabled. And if two columns are disabled, responsive becomes 2 columns. Each display (display1,2,3,4) represents a column. – Hadi Aug 17 '18 at 10:28