Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use php variables from an included php file

I have these variables that are set in one php file, and when i include that php file in another php file, how do i use those variables from the included php file?

like image 520
dave Avatar asked Nov 29 '22 04:11

dave


2 Answers

They should just roll over:

File1.php

<?php
$var1 = "TEST";
?>

File2.php

<?php
include("File1.php");
echo $var1; //Outputs TEST
?>
like image 78
Ben Avatar answered Dec 05 '22 02:12

Ben


Have you actually tried it?

Just use the variables. They are available within the scope of the including file.

From the PHP manual:

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

like image 40
Aron Rotteveel Avatar answered Dec 05 '22 04:12

Aron Rotteveel