Error adding file inside function

1

I am building a system where I want to do it multiple languages ... Well I created the project, inside it has a folder strings (where inside has a file called PT-BR.php), has a file functions (where it has a function that passes the filename and it includes with the include_once), in the file that I'm doing the test by including and calling the function it all normal, but when I echo an var that is inside the file, the error. How can I solve ? or is there an easier method? Thanks

test.php file

<?php
include_once ("class/funcoes.php");
autoLoad();
session_start();
$sl = $_SESSION['lang']; //Passa PT-BR
defineLanguage($sl);
echo $strNome;

file class / funcoes.php

<?php

    function defineLanguage ($language) {
        $dir = "strings/";
        if($language == "PT-BR") {
            $include = $dir . "PT-BR" . ".php";
            //include_once($include);
            include('strings/PT-BR.php');
        }
    }

file strings / PT-BR.php

<?php
$strNome = "teste";

ERROR

  

Notice: Undefined variable: strName in /Applications/XAMPP/xamppfiles/htdocs/log-u/testeSession.php on line 11

    
asked by anonymous 12.02.2016 / 18:14

1 answer

1

The problem is that include is done inside the function, so all variables defined in it and the include variables are restricted to the function as nothing is returned undefined variable occurs.

Function:

function defineLanguage ($language) {
    $dir = "strings/";
    if($language == "PT-BR") {
        return $dir . "PT-BR" . ".php";
    }
}

Call:

<?php
include_once ("class/funcoes.php");
autoLoad();
session_start();
$sl = $_SESSION['lang']; //Passa PT-BR
$arquivo = defineLanguage($sl);
require_once($arquivo); //<--- linha adicionada
echo $strNome;
    
12.02.2016 / 18:28