views:

180

answers:

7

Hey i have a question. At the moment i'm trying to use a stylesheet which i get through a if. but it doesn't do anything.

here is my code at the moment. the variable $stylesheet will be variable but while testing i've setted it to normal

    <?php
    $stylesheet =  'normal'
if($stylesheet = 'small')
{
    $style = './sitestyle/stylesheetsmall.css';
}
if($stylesheet = 'big')
{
    $style = './sitestyle/stylesheetbig.css';
}
    else
    {
            $style = './sitestyle/stylesheet.css';
    }

    echo '<link rel="stylesheet" type="text/css" href="$style">';
    ?>

Thanks for your answers.

+6  A: 

When comparing things, use == in stead of =.

$a=0; if( $a = 1 ) { echo "1"; } else { echo "not 1"; }

The if( $a = 1 ) will use the return value of $a=1 as a condition, in this case the return value is $a, which equals 1.

xtofl
To continue... if you use the `=` for comparison, it will always evaluate as `true` because you are testing to see if an assignment succeeded. Actually, if you tried something like `if ('test' = $test)...` that might evaluate as `false` because you can't assign to a constant.
FrustratedWithFormsDesigner
... And sometimes it's even better to use `===`. http://www.php.net/manual/en/language.operators.comparison.php
xtofl
@xtofl: I'd forgotten all about `===`! Ok, who wants to define a `====` operator? you can never have too many `=`'s! :P
FrustratedWithFormsDesigner
@FrustratedWithFormsDesigner: "it will always evaluate as true because you are testing to see if an assignment succeeded.". You're not testing whether the assignment succeeded, but testing the value that was assigned. `($a = $b) === $b`; `($a = $b) == true` only if `$b` happens to convert to bool `true`.
Tom Haigh
A: 

If you want to check if a value is equal to another, use double equal sign ==

$stylesheet == 'small'

Alexander
+2  A: 

You're using PHP's assignment operator instead of the equals operator for your comparisons:

if ($stylesheet = 'big')

That code actually assigns the value 'big' to $stylesheet and the result of the assignment is then evaluated by the if as a boolean (this would be true in PHP).

You're going to want to change the = to == so that the expression is evaluated directly as a boolean instead of evaluating the result of the assignment:

if ($stylesheet == 'big')
Sean Bright
+1  A: 

you can use switch for this

switch ($size) {
    case 'small':
        # small css code...
        break;

    case 'big':
        # big css code...
        break;

    default:
        # default stylesheet code...
        break;
}
Osman Üngür
+6  A: 

Ok, so as the others have said, = is assignation, == is comparision.

But your problem could be simplified by using a switch statement:

$stylesheet =  'normal';
switch($stylesheet) {
    case 'small':
        $style = './sitestyle/stylesheetsmall.css';
        break;
    case 'big':
        $style = './sitestyle/stylesheetbig.css';
        break;
    default:
        $style = './sitestyle/stylesheet.css';
        break;
}
echo '<link rel="stylesheet" type="text/css" href="'.$style.'">';
ILMV
+1 for the switch, but $style will never be in the output as you're using single quotes around the string that's echoed
Cez
thank you for this. with the echo output of scott it works perfect : )
mikep
To be fair I just copied and pasted the authors code, but you're right, the `$style` needs to be escaped.
ILMV
+3  A: 

Others have pointed out the problem with your if statements. You also have a problem with your echo statement. Variables will not be looked at within single quotes. You need to move the variable out of the quotes and concatenate, or change to double quotes and escape all your other double quotes. I prefer the first method:

echo '<link rel="stylesheet" type="text/css" href="' . $style . '">';
Scott Saunders
+1, well observed
ILMV
A: 

You could also forgo the switch/if entirely.

<link rel="stylesheet" type="text/css" href="./sitestyle/stylesheet<?echo $style;?>.css">

Just make sure $style is only ever set to "", "big", or "small"

aslum