PHP:cookie解析错误

时间:2012-01-14 11:38:47

标签: php parsing cookies

当cookie存在时,我对cookie上的文本有问题,我收到一个错误:“解析错误:语法错误,意外T_VARIABLE,期待” if $_COOKIE['style'] == 'styles2'

<?php 
    if (!isset($_COOKIE['style']))
        echo "<option value='styles' selected>Style 1</option>
              <option value='styles2'>Style 2</option>";
    else{
        if $_COOKIE['style'] == 'styles2'
            echo "<option value='styles'>Style 1</option>
                  <option value='styles2' selected>Style 2</option>";
        else
            echo "<option value='styles' selected>Style 1</option>
                  <option value='styles2'>Style 2</option>";
    }
?>

2 个答案:

答案 0 :(得分:1)

表达式需要括号:

if ( $_COOKIE['style'] == 'styles2')

答案 1 :(得分:0)

不要重复自己。

<?php 
$style = '';
if (isset($_COOKIE['style'])) $style = $_COOKIE['style'];
?>
<option value='styles'>Style 1</option> 
<option value='styles2'<?php if ($style == 'styles2') { ?> selected<?php } ?>>Style 2</option>