0

why variable $lang_add doesn't work in function below? Do I need declare it like add_button($lang_add)?

function add_button($module) {
echo '<a href="?do=add" class="button">',$lang_add,'</a>';
}

EDIT: My code now is:

function add_button($module) {
require("languages.php");
echo '<a href="?do=add" class="button">',$lang_add,'</a>';
}

$lang_add is defined in languages.php and still it doesn't show in function.

Damian Hetman
  • 372
  • 4
  • 20
  • Just try it ;-) but yes, that's what you need. Furthermore, it's better practise to `return` from the function, and echo it in the global scope – Qirel Sep 18 '17 at 09:50
  • Ok. But I think I may have other problem. When I use code below I don't get $lang_add variable ($lang_add is defined in language.php): `function add_button($module) { require("languages.php"); echo '',$lang_add,''; }` – Damian Hetman Sep 18 '17 at 10:04
  • It's within the functions *scope*. The function only cares about variables that you passed through its signature (`function add_button($module) {` (like `$module` there, you can use that variable inside the function - but nothing else)). When you use it, you can do `add_button($module)`, but inside the function, you need to use the same name as its defined with in the signature. That being said, you should return from a function, and not echo from it - echo in the global scope. Gives you more control over whats printed where. – Qirel Sep 18 '17 at 10:07
  • So, baiscally you need to 1) read this: http://php.net/manual/en/functions.arguments.php and 2) define the function as `function add_button($lang_add) {` – Qirel Sep 18 '17 at 10:08

0 Answers0