我正在使用codeigniter框架。我在$this->session->userdata('sess_place')
中设置了会话,但我无法使用if
条件进行回应。也许我的if
条件错了?
$pl = $this->session->userdata('sess_place');
if(isset($pl) && $pl = TRUE) {
echo $pl;
}
else {
echo "Select your city";
}
答案 0 :(得分:2)
您使用了assign operater(=)而不是==,这是错误的。试试这个:
$pl = $this->session->userdata('sess_place');
if(isset($pl) && $pl == true) // and also no need of isset($pl) here jusy if($pl == TRUE) is enough
{
echo $pl;
}
else
{
echo "Select your city";
}
答案 1 :(得分:2)
这应该适合你:
$pl = $this->session->userdata('sess_place');
if(!empty($pl)) {
echo $pl;
} else {
echo "Select your city";
}
答案 2 :(得分:1)
尝试从会话中获取数据。如果它返回false以外的其他内容,则必须设置它。尝试使用 -
if ($this->session->userdata('sess_place')) {
echo $this->session->userdata('sess_place');
} else {
echo "Select your city";
}
答案 3 :(得分:0)
如果您在会话中存储布尔值,请尝试此操作。
$pl = $this->session->userdata('sess_place');
if(isset($pl) && $pl === true) // this checks value and its data type
{
echo "Got it";
}
else
{
echo "Select your city";
}