PHP include not work as it should
Get the solution ↓↓↓i am including lang from separate file. How ever I am getting error and I don't know why. This code below display me correct translate for $lang['YES'] if language of borowser is SK, as soon as it's CS it display error: Undefined variable: lang .
First i was thinking problem can be in translated files so i COPY / PASTE sk.php and rename it to cs.php , still getting same error.
Echo on files in my code display this in CS:
1.$user_lang = cs,sk-SK;q=0.5
2.$user_lang = cs
3.$lang_file = cs.php
The path is correct because it works for sk.php, the cs.php file is there as well becuase else it will display error with include. (i check it as well to make sure), how ever i still get my error. Can somebody give me some help?
$user_lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$_SESSION['lang'] = $user_lang;
setcookie('lang', $user_lang, time() + (3600 * 24 * 30));
echo $user_lang;
$user_lang = substr($user_lang, 0, 2);
echo "<br>";
echo $user_lang;
switch ($user_lang)
{
case 'sk':
$lang_file = 'sk.php';
break;
case 'cs':
$lang_file = 'cs.php';
break;
default:
$lang_file = 'cs.php';
}
echo "<br>";
echo $lang_file;
echo "<br>";
$include_lang_path = 'languages/'.$lang_file;
echo $include_lang_path;
include_once $include_lang_path;
echo $lang['YES']
Thank you all for reading this!
Answer
Solution:
may you try this here...
$include_lang_path = './languages/' . $lang_file;
include_once( $include_lang_path );
or
include_once( './languages/' . $lang_file );
whats actually is the same. Allways use the./
in front.
You could also try
include( $include_lang_path );
instead of
include_once $include_lang_path;
The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once.
so may there was some included before.
Answer
Solution:
There can be many possibilities for your issue:
What doesecho $lang_file;
output? First, make sure that file exists.
Also, check if the directory(languages) exist in the PHP'sinclude_path
configuration.
If not, you can try adding.
in front of the path you have mentioned if the path is relative to the current file, like
$include_lang_path = './languages/'.$lang_file;
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: illegal string offset
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.