mikepenz
mikepenz

Reputation: 12858

Change Stylesheet via If in php

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.

Upvotes: 1

Views: 4130

Answers (8)

user5365803
user5365803

Reputation:

$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 . '">';

one equals is for assigning thing in php == is for if it is equal to and === is for if it is exactly equal to

Upvotes: 1

aslum
aslum

Reputation: 12254

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"

Upvotes: 0

Ben Everard
Ben Everard

Reputation: 13804

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.'">';

Upvotes: 9

Scott Saunders
Scott Saunders

Reputation: 30394

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 . '">';

Upvotes: 3

osm
osm

Reputation: 4218

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;
}

Upvotes: 1

Sean Bright
Sean Bright

Reputation: 120644

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')

Upvotes: 2

Alex
Alex

Reputation: 14618

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

$stylesheet == 'small'

Upvotes: 0

xtofl
xtofl

Reputation: 41509

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.

Upvotes: 6

Related Questions