oscarmarcelo
oscarmarcelo

Reputation: 91

Return an array value from included file in a function

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...

Upvotes: 0

Views: 144

Answers (2)

Ben
Ben

Reputation: 56

$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.

Upvotes: 2

John Conde
John Conde

Reputation: 219804

Your syntax is wrong:

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

should be

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

Upvotes: 2

Related Questions