Welcome Guest, Not a member yet? Register   Sign In
Differences in session variable declaration
#1

Hi, I have the following hook

PHP Code:
class LanguageLoader
{
 
 function initialize() {
 
   $ci =& get_instance();
 
   $ci->load->helper('language');

 
   $site_lang $_SESSION['site_lang']; 
 
   //$site_lang = $ci->session->site_lang;
 
   if ($site_lang) {
 
       $ci->lang->load('general'$site_lang);
 
   } else {
 
       $ci->lang->load('general''english');
 
   }
 
 }



With the line
PHP Code:
$site_lang $_SESSION['site_lang']; 
I get the following error

PHP Code:
A PHP Error was encountered
Severity
Notice

Message
Undefined indexsite_lang

Filename
hooks/LanguageLoader.php

Line Number
12

Backtrace
:

FileC:\xampp\htdocs\availability-calendar\application\hooks\LanguageLoader.php
Line
12
Function: _error_handler

File
C:\xampp\htdocs\availability-calendar\index.php
Line
315
Function: require_once 

but replacing it with
PHP Code:
$site_lang $ci->session->site_lang
resolves the problem. I'm curious to know what the issue was though. Thanks.
Reply
#2

When using:
PHP Code:
$CI =& get_instance(); 

Your getting CodeIgniter's Controller Class.

Are you loading the Session library in the Controller or are you Autoloading it?
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

@n2fole00

It could be what value is returned between the two. You could do a var_dump on both values and see what is returned that should give you the answer that you seek.
Reply
#4

(09-12-2018, 02:20 PM)InsiteFX Wrote: When using:
PHP Code:
$CI =& get_instance(); 

Your getting CodeIgniter's Controller Class.

Are you loading the Session library in the Controller or are you Autoloading it?

I'm autoloading it.
Reply
#5

(09-12-2018, 02:22 PM)php_rocs Wrote: @n2fole00

It could be what value is returned between the two.  You could do a var_dump on both values and see what is returned that should give you the answer that you seek.

They both return the same thing. The error occurs when the value is NULL.
Reply
#6

Then use isset method on it to see if it contains a value or not.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

@n2fole00,

They both are null but one causes an errors and the other doesn't? That can't be right. There is a difference. That is the only reason why one would work and the other not work.

InsiteFX made a excellent suggestion as well.
Reply
#8

(This post was last modified: 09-13-2018, 02:09 PM by n2fole00.)

(09-13-2018, 07:51 AM)php_rocs Wrote: @n2fole00,

They both are null but one causes an errors and the other doesn't?  That can't be right.  There is a difference.  That is the only reason why one would work and the other not work.

InsiteFX made a excellent suggestion as well.

This is what I was thinking, but what could be the difference? var_dump should give me the output isset would test for.
Reply
#9

@n2fole00,

What was the exact var_dump for each variable?
Reply
#10

(09-13-2018, 02:08 PM)php_rocs Wrote: @n2fole00,

What was the exact var_dump for each variable?

PHP Code:
$site_lang2 $_SESSION['site_lang'];
$site_lang $ci->session->site_lang;

echo 
"site_lang: ";
var_dump($site_lang);
echo 
"site_lang2: ";
var_dump($site_lang2);
echo 
"isset: ";
var_dump(isset($site_lang2)); 

Code:
site_lang: NULL site_lang2: NULL isset: bool(false)
Reply




Theme © iAndrew 2016 - Forum software by © MyBB