通过在PHP中更改样式表

mik*_*enz 1 css php variables if-statement stylesheet

目前我正在尝试使用样式表,我通过if.但它没有做任何事情.

这是我目前的代码.变量$ stylesheet将是可变的,但在测试时我已将其设置为正常

<?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">';
?>
Run Code Online (Sandbox Code Playgroud)

谢谢你的回答.

Ben*_*ard 9

好吧,正如其他人所说,=是分配,==是比较.

但是使用switch声明可以简化您的问题:

$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.'">';
Run Code Online (Sandbox Code Playgroud)


xto*_*ofl 6

在比较事物时,请使用==而不是=.

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

if( $a = 1 )将使用的返回值$a=1作为条件,在这种情况下,返回值是$a,它等于1.

  • 要继续...如果使用`=`进行比较,它将始终评估为"true",因为您正在测试是否成功分配.实际上,如果你尝试了类似`if('test'= $ test)...`的东西,可能会评估为'false`,因为你无法分配常量. (3认同)