0

Ok, stupid question, I guess...

I'm trying to do this:

File: pt.php

<?php $langlist = array ( "Car" => "Carro", "Big Car" => "Carro grande") ?>

File: index.php

<?php
$lang = 'pt';
if ($lang != 'en') include('locale/' . $lang . '.php');
function __($langstring){
    if ($lang != 'en'){
        echo $langlist[$langstring];
    } else {
        echo $langstring;
    }
}
?>

But this doesn't work (Notice: Undefined variable: lang and langlist).

What am I doing wrong?

P.S.: I know using echo instead of return inside a function ins't correct, but I don't want do be using echo __(); every time I need to use this function...

2 Answers 2

2

$lang and $langlist are global variables, but they cannot be seen from within the function. Simply add the following as the first line of the function to gain access to them:

global $lang, $langlist;

Alternatively, you could access them as $GLOBALS['lang'] and $GLOBALS['langlist'] without using the global declaration.

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

2 Comments

It doesn't fully solves the problem. After the 12th time I use the __() function, it displays a "Notice: Undefined index: Array Key".
My bad! I forgot that I was using the $langlist twice.
2

Your syntax is wrong:

<?php $langlist = array { "Car" => "Carro", "Big Car" => "Carro grande"} ?>

should be

<?php $langlist = array("Car" => "Carro", "Big Car" => "Carro grande") ?>

1 Comment

Oops! My bad! This error is only in this question. The code already have the correct syntax. I guess this isn't the source of the problem... ;) (Corrected syntax in question.)

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.