1

So I have a stylesheet that i've written in PHP as well as a settings file that is included at the top of the stylesheet and holds some variables and functions that define some of my css properties. For example:

// Background color for body element
// Type: String
$bodyBGColor = 'rgb(67,142,169)';

// Has background Image for body element
// Type: Bool
$bodyBGIMG = false;

function hasBGIMG(){
    if($bodyBGIMG){
        echo "url(../img/".$adPrefix."_BG.png) !important";
    }else{
        echo " ";
    }
};

In the CSS I use the hasBGIMG() function.

body{
background-color: <?php echo $bodyBGColor.' !important' ?>;
background: <?php hasBGIMG(); ?>;
}

The first time I created and used the function it worked great, but when I change the value of $bodyBGIMG to true the CSS files still shows the value for false.

Here's the output in the css file:

body{
background-color: rgb(67,142,169) !important;
background:  ;
}

I have other functions in the file that behave the same way. I can even add new variables and functions to the file and they get parsed properly and yet any old functions keep their old values.

Any thoughts on why the function isn't looking at the updated variables or why the stylesheet is keeping the old values?

Thanks

4 Answers4

2

The variable $bodyBGIMG inside the function definition is local to the function (not global). Therefore, you must either call global $bodyBGIMG or pass it in as a function parameter:

Solution using global variable:

// Variable $hasBGIMG must be defined before the global keyword is called.
function hasBGIMG(){
    global $bodyBGIMG;
    if($bodyBGIMG){
        echo "url(../img/".$adPrefix."_BG.png) !important";
    }else{
        echo " ";
    }
};

Solution with passing the value to the function:

function hasBGIMG($bodyBGIMG){
    if($bodyBGIMG){
        echo "url(../img/".$adPrefix."_BG.png) !important";
    }else{
        echo " ";
    }
};

and later on:

// Variable $bodyBGIMG must be defined before function hasBGIMG is called.
background: <?php hasBGIMG($bodyBGIMG); ?>;
Stegrex
  • 3,991
  • 1
  • 14
  • 19
1

Unlike in JavaScript, variables in PHP do not "cascade" into functions.

To access the $bodyBGIMG variable inside the function, you need to either pass it as a parameter, or start your function with:

global $bodyBGIMG;
Niet the Dark Absol
  • 301,028
  • 70
  • 427
  • 540
1

Unlike Javascript, PHP is not a language that supports lexically scope (outside the use of a use clause on a closure definition). If you want access to variables inside of a function you have to pass them into the function.

$bodyBGColor = 'rgb(67,142,169)';

// Has background Image for body element
// Type: Bool
$bodyBGIMG = false;

function hasBGIMG($bodyBGIMG, $adPrefix){
    if($bodyBGIMG){
        echo "url(../img/".$adPrefix."_BG.png) !important;";
    }else{
        echo " ";
    }
};
Orangepill
  • 23,853
  • 3
  • 38
  • 62
1

You have done two mistakes here.

1. You are not passing variable as function parameter, and because of that it always evaluates to false.

2.; at end of function.

 $bodyBGIMG = false;

function hasBGIMG($bodyBGIMG,$adPrefix){
    if($bodyBGIMG){
        echo "url(../img/".$adPrefix."_BG.png) !important";
    }else{
        echo " ";
    }
}

Are global variables in PHP considered bad practice? If so, why?

Community
  • 1
  • 1
Yogesh Suthar
  • 29,554
  • 17
  • 66
  • 96