-1

My php script includes another en.php file which contains the required english strings. This same page also calls a html page which uses the file and formats it using the contents of the en.php file.

I have a function in this script which references variables defined in the included script but I am getting error messages of the variable not being found. If I reference the variable outside the function, the variable is accessed correctly. Why can I not access these variables inside the function?

en.php

<?php
   $lang_yes = 'Yes';
   $lang_no = 'No';
?>

example.php

<?php
include_once('addons/assq/lang/en.php');

echo $lang_yes;
$q1 = convertToYesOrNoString(0);
echo $q1;

function convertToYesOrNoString($value){
    //include_once('addons/assq/lang/en.php');
    if ($value == 0){
        return $lang_no;
    }else if ($value == 1){
        return $lang_yes;  
    }else{
        return "---";
    }
}
?>

My output is as follows:

Yes

Undefined variable: lang_no in example.php on the line in the function 

I tried including the en.php directly into the function but that did not work either. How can I access these variables inside my function while including the file as implemented above?

mcv
  • 1,232
  • 2
  • 16
  • 39

2 Answers2

1

That's a scope issue. That variable $lang_no will not be accessed under that function , you need to pass that as a parameter instead to the function definition.

function convertToYesOrNoString($value,$lang_no){ //<--- Like this.

Since you have mentioned that you have a lot of parameters .. you can write a turnaround like this...

Your en.php

<?php
//Map all those variables inside an array as key-value pair. as shown
$varArray=array('lang_yes'=>'Yes','lang_no'=>'No'); 

Some test.php

<?php

include('en.php');
function convertToYesOrNoString($varArray)
{
 extract($varArray);
 echo $lang_yes; // "prints" Yes
 echo $lang_no;  // "prints" No
} 

convertToYesOrNoString($varArray);

Shankar Damodaran
  • 65,155
  • 42
  • 87
  • 120
  • is there a way that I could include that file within the scope of the function? Passing in the variable seems like overkill. I only displayed a quick example, but I could be passing in more 2 variables. – mcv Mar 03 '14 at 15:30
  • actually I would have to pass in the following... function convertToYesOrNoString($value,$lang_no,$lang_yes){ – mcv Mar 03 '14 at 15:36
  • Yes that is better indeed , just see here why you should not use the global keyword. http://stackoverflow.com/questions/1557787/are-global-variables-in-php-considered-bad-practice-if-so-why – Shankar Damodaran Mar 03 '14 at 15:38
  • 1
    The en.php file is a language property file. If I limit the use of global only to this file I don't forsee it becoming a huge problem. There is no ties to indexes of arrays. I could of hard coded the values, but than I would have to make changes in more than one location if I did that. I'm sort of surprised that a function call within a script is excluded from the scope. I thought by adding an include_once inside the function would resolve it but it didn't. – mcv Mar 03 '14 at 16:05
1

You can either define it as a constant, pass it as an argument or declare it as a global within the function:

function convertToYesOrNoString($value){
    global $lang_no, $lang_yes;
    //...
}
Adam Hopkinson
  • 26,835
  • 7
  • 57
  • 87