0

I have 2 php files inside joomla and I want to make a variable from php-file 1 available in php-file 2.

php-file_1.php

$a_variable = $input->getString('text');

php-file_2.php

include(php-file_1.php);
echo $a_variable;

I get Notice: Undefined variable: a_variable in /var/www/vhosts/a_domain.de/httpdocs/php-file_2.php on line 2

what's wrong here?

  • Try include "php-file_1.php"; – Mamphir Oct 16 '20 at 20:59
  • I just searched stackoverflow for "php access variable from another file". There are lots of answers. Have you tried searching for an answer to your question? – Cully Oct 16 '20 at 21:00
  • Check the answer on this post, this might help: https://stackoverflow.com/questions/13135131/php-getting-variable-from-another-php-file – Tom Oct 16 '20 at 21:02

1 Answers1

2

include and require are not functions, they are language constructs. Therefore they need to be used without brackets.

Either way, you missed quotes around php-file_1.php.

include 'php-file_1.php'; should work fine.

dmuensterer
  • 1,875
  • 11
  • 26
  • You can use it with or without parentheses. It works the same. – Cully Oct 16 '20 at 21:14
  • Yes, but you shouldn't. "Because include is a special language construct, parentheses are not needed around its argument. " as per https://www.php.net/manual/en/function.include.php – dmuensterer Oct 16 '20 at 21:24
  • it doesn't work. I included php-file_1.php in php-file_2.php and the variable isn't available in php-file_2.php. this isn't the way it should be. – Benjamin Röhling Oct 17 '20 at 00:11
  • should I open another question for the global variable question? – Benjamin Röhling Oct 17 '20 at 00:18
  • @BenjaminRöhling You missed quotes around your php file. Please have another look at my answer, because I tried your exact code and it works. – dmuensterer Oct 17 '20 at 15:10
  • yeah! I missed them here but not in the source code. the problem was multi-problems. I'm not a novelist so I can't list them here. I'm taking a pbreak from programming. – Benjamin Röhling Oct 18 '20 at 17:14