0

I have a function called

chewbacca() {
    include('external.php');
    echo $lang[1];
    ...
    }

The file external.php contains all the $lang array. However, since I have to execute the function thousands of times, I would like to include only once the file. If I include_once('external.php'); before the function, how can I use the $lang array variables in my function without having to write "global" before each use?

5 Answers 5

2

Maybe passing it as an argument?

<?php

include 'external.php';

function chewbacca($lang_array){
    echo $lang_array[1];
    //...
}

Edit:

You could do the following too:

On external.php:

<?php

return array(
    'foo',
    'foobar',
    'bar',
);

On index.php:

<?php

function chewbacca($lang_array){
    echo $lang_array[1];
    //...
}

$foo = include 'external.php';
chewbacca($foo);

Edit2: Of course now you can use include_once, but I would recommend require_once because you won't have the array if the include fails and the script should stop with an error.

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

3 Comments

Wow i got my certified engineer certificate and i have never seen or used the "$foo = include(file)" before. Does that really work? I thought include was a language construct that returned nothing...
@Paul Oh well, i just read it, if you "return" something in the included file, it returns it instead of integrating the code into the current location... nice one
@MathieuDumoulin Of course it works: php.net/manual/en/function.include.php
1

Unless I'm misunderstanding what you're after, you don't need to write global before each use, you just have to use it at the start of the function.

include('external.php');

chewbacca() {
    global $lang;
    echo $lang[1];
    ...
}

Comments

1

Simply said, you can't...

You have several ways to do this:

Way #1

global $lang;
include('external.php')
function chewbacca(){
    global $lang;
    echo $lang[1];
}

Way #2

function chewbacca(){
    include('external.php')
    echo $lang[1];
}

Way #3

function chewbacca(){
    static $lang;
    if(!is_array($lang)){ include('external.php'); }
    echo $lang[1];
}

Way #4

include('external.php')
function chewbacca($lang){
    echo $lang[1];
}
chewbacca($lang);

Good luck

PS: Another way would be to use a CLASS a load the strings in the class when it gets created (inside the constructor) and access the language strings from $this->lang...

Comments

1

Static class also is a solution.

class AppConfiguration {
    static $languages = array(
      'en' => 'English'  
    );
}

function functionName($param) {
    $lang = AppConfiguration::$languages;
}

require_once that class in document and that's it.

Comments

0

If I understood you correctly, try and pass it to a local scope before using it; that way you'll only need to use the global scope once inside the function.

Comments

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.