开发者

Change Stylesheet via If in php

开发者 https://www.devze.com 2022-12-28 03:04 出处:网络
At the moment i\'m trying to use a stylesheet which i get through a if. but it doesn\'t do anything.

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 = './s开发者_Python百科itestyle/stylesheetbig.css';
    }
  else
    {
    $style = './sitestyle/stylesheet.css';
    }

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

Thanks for your answers.


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


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.


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


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


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


$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


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

$stylesheet == 'small'


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"

0

精彩评论

暂无评论...
验证码 换一张
取 消