1

How to pass an array from one file to another using include using PHP language? I have one file with some language array(language/langen.php):

global $lang;
$lang['Here'] = 'Here';
$lang['Date'] = "Date";

In other file I have:

include base_url().'language/lang'.$_COOKIE['lang'].'.php';
var_dump($lang);

*(My mistake by coping code - true is var_dump($lang))* But it shows me an error:

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: lang

How to solve this problem and what did I do wrong?

1
  • 1
    I suppose you don't have such cookie as "lang" in your browser. That's what is says. Try echo $_COOKIE['lang'] and see for yourself. Commented Nov 28, 2012 at 22:28

3 Answers 3

1

First of all: You should never use cookie value directly in include statement - its pretty easy to make fake cookie and mess up in your application.

I suppose you just don't have cookie with name lang or variable $lang has never been initialized before.

To check if cookie exists and is in correct format you can do like that:

// set default lang code
$langCode = 'en';

// check if cookie exists and if contains string build from 2 characters from range a-z
// check also if file exists
if (isset($_COOKIE['lang'] && preg_match('/^[a-z]{2}$/', $_COOKIE['lang']) &&
    file_exists(base_url().'language/lang'.$_COOKIE['lang'].'.php')) {
    $langCode = $_COOKIE['lang'];
}
include base_url().'language/lang'.$langCode.'.php';

in included file you should check if variable $lang exists

if (!isset($lang)) $lang = array();
$lang['Here'] = 'Here';
$lang['Date'] = "Date";

also I think using global here is pointless as from your example it looks like its the same scope.

anyway for me much cleaner solution would be:

// first_file.php
$langCode = 'en';
if (isset($_COOKIE['lang'] && preg_match('/^[a-z]{2}$/', $_COOKIE['lang']) &&
    file_exists(base_url().'language/lang'.$_COOKIE['lang'].'.php')) {
    $langCode = $_COOKIE['lang'];
}
$lang = include base_url().'language/lang'.$langCode.'.php';


// langen.php
return array(
    'Date' => 'Date',
    'Here' => 'Here',
);

EDIT

One more thing - If base_url() is returning web URL (like http://example.com...) then it is also wrong (and also can cause problem as langen.php will contain at least Notice message when included this way) - should be included with valid file path

Sign up to request clarification or add additional context in comments.

3 Comments

Hey, that "first of all" line was mine :] On the other hand, your cleaner solution truly deserves +1.
heh, sorry, looks like we though in same direction ;)
"One more thing - If base_url() is returning web URL (like example.com...) then it is also wrong (and also can cause problem as langen.php will contain at least Notice message when included this way) - should be included with valid file path" - This is solved my error. When I change include base_url().'language/lang'.$langCode.'.php'; into include 'language/lang'.$langCode.'.php'; works fine! Thank you so much lupatus!
0

Shouldn't it be?

include base_url().'language/lang'.$_COOKIE['lang']['Here'].'.php';

Or else it would just return array()

1 Comment

In $_COOKIE['lang'] I put the value of language file which php will include. When I print_r($_COOKIE['lang']) it gives me an value 1 which is name of language file(lang1.php).
0

First of all, I don't see the point of var-dumping variable $data, if there is no such thing in code you posted. If you have a cookie called "lang" in your browser, then you should be fine. You could always check this by

var_dump($GLOBALS['lang']);

in your code. It should print the array of values from your lang*.php file.

2 Comments

var_dump($GLOBALS['lang']); it doesn't print me a values from my lang*.php. It shows me only an error:"A PHP Error was encountered Severity: Notice Message: Undefined index: lang". I put it after include file.
It's quite right to put it after include, but if you can't include your file, then nothing is going to print, believe me. Is something printing out if you include your file directly (without this cookie line?). I simulated the situation and it works just fine on my side. Isn't there something in your code (above these lines) you are not telling us, which could cause such problems?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.