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?

3
  • Try include "php-file_1.php"; Commented Oct 16, 2020 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? Commented Oct 16, 2020 at 21:00
  • Check the answer on this post, this might help: stackoverflow.com/questions/13135131/… Commented Oct 16, 2020 at 21:02

1 Answer 1

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.

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

6 Comments

You can use it with or without parentheses. It works the same.
Yes, but you shouldn't. "Because include is a special language construct, parentheses are not needed around its argument. " as per php.net/manual/en/function.include.php
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.
should I open another question for the global variable question?
@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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.