WasteD WasteD - 14 days ago 8
PHP Question

Can I use variables of a included file in another included file?

Hello let me explain my question. I'm using PHP at the moment and I'm just playing around a bit now im wondering if I have a file for example:

//This is file1.php
<?php
$text = "Hello";
?>


And include it in file2 and also include file3:

//This is file2.php
<?php
include 'file1.php';
include 'file3.php';
?>


Now Comes my question can I use $text in file3 like so?:

//This is file3.php
<?php
echo $text;
?>


Thank you for your response!

Answer

Short ans quick: Yes you can, but NOT in your example. If you call $text in your file2 after including it, it works.

Include does the same, as you have the code in the same file. But look at the order, its important.

Build one file you call over the browser and require all you need in this file.