开发者

Php variables end up NULL (but the code works)!

I use the following code to change the language in my website:

<?php
session_start();
header('Cache-control: private'); // IE 6 FIX

function get_lang(){
    if(!empty($_GET['lang'])) return $_GET['lang'];
    if(!empty($_SESSION['lang'])) return $_SESSION['lang'];
    if(!empty($_COOKIE['lang'])) return $_COOKIE['lang'];
    return 'en';
}

function set_lang($lang){
    setcookie("lang", $lang, time() + (3600 * 24 * 30));
    $_SESSION['lang'] = $lang;
}

function get_lang_file($lang){
    $lang_file = "languages/lang.$lang.php";
    if(file_exists($lang_file)) return $lang_file;
    if($lang_file = get_lang_file('en')) return $lang_file;
    return false;
}

//translation helper function
function l($string){
    static $translation;

    if(!isset($translation)){
        $lang = get_lang();
        $lang_file = get_lang_file($lang);
        if($lang_file) set_lang($lang);
        $translation = include $lang_file;
    }

    return $translation[$string];
}

?>

I want to add a Javascript file according to the value of one of the variables, in this case $lang_file:

    <?php if($lang_file=='lang.en.php') {echo ' <script type="text/javascript" src="scripts/jquery-validate/val-en.js"></script>';} ?>
    <?php if($lang_file=='lang.es.php') {echo ' <script type="text/javascript" src="scripts/jquery-validate/val-es.js"></script>';} ?>
    <?php if(开发者_Python百科$lang_file=='lang.tw.php') {echo ' <script type="text/javascript" src="scripts/jquery-validate/val-tw.js"></script>';} ?>
    <?php if($lang_file=='lang.cn.php') {echo ' <script type="text/javascript" src="scripts/jquery-validate/val-cn.js"></script>';} ?>

But it doesn't work because all the variables become NULL at the end (but the code works).

I tested it with this and it says NULL NULL NULL NULL:

var_dump($translation);
var_dump($lang);
var_dump($string);
var_dump($lang_file);

Any suggestions?

EDIT: sample of the Javascripts I'm calling (val-xx.js):

$(document).ready(function(){
    $("#sendmail").validate({
        rules: {
            FieldData0: {
                required: true
            },
            FieldData1: {
                required: true,
                email: true
            },
            FieldData2: {
                required: true
            }
        },
        messages: {
            FieldData0: {
                required: "Please enter your name"
            },
            FieldData1: {
                required: "Please enter your email address"
            },
            FieldData2: {
                required: "Please enter a message"
            }
        }
    });
})

a sample of a lang.$lang.php file:

<?php
return array(
'tagline_h2' => '我创造简单...',

(and so on)


It looks like you're a victim of variable scope. If you want to reference a variable defined in a function you have to declare it as global. For example:

$lang_file = "languages/lang.en.php" //default
function get_lang_file($lang){
    global $lang_file;
    $lang_file = "languages/lang.$lang.php";
    if(file_exists($lang_file)) return $lang_file;
    if($lang_file = get_lang_file('en')) return $lang_file;
    return false;
}

OR (more properly), just use the functions return value:

if(get_lang_file($mylang) == 'blah')

versus

if($lang_file == 'blah')


It looks like you're setting $lang_file to "languages/lang.$lang.php". In your echo string you should use the following:

<?php if($lang_file=='languages/lang.en.php') {echo '<script type="text/javascript" src="scripts/jquery-validate/val-en.js"></script>';} ?>


Your functions are very nicely defined, but they never actually run, unless you're omitting something. Even then, only the values that are being returned from a function would survive, variables inside a function are not valid outside of it.


$translation is static and all of the rest are local, so none of them are accessible at global scope. They are not "becoming" NULL, they simply don't exist (so PHP thinks you're referring to new variables).

Basically what Benjamin just said, except that to do the comparison you seem to want to be doing you should be just calling the get_lang() function.


Variables in functions are in its "local scope", which cannot be used in "global scope". Please refer Benjamin Manns's reply.

In short, you need a glue:
1. Using global variables in functions, or
2. Using a code block to glue them, or
3. Using a wrapper function similar to 2 to make it work.

A sample code block of (2)

// After the function block

$lang = get_lang();

set_lang($lang); // For repeating visit

$lang_file = get_lang_file($lang);

if ($lang_file !== FALSE)

{

   switch ($lang_file)
   {
        case 'lang.cn.php': 
                   echo ' <script type="text/javascript" src="scripts/jquery-validate/val-cn.js"></script>';
           break;
           // Other cases

     }

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜