我可以在另一个包含的文件中使用包含文件的变量吗?

我可以在另一个包含的文件中使用包含文件的变量吗?

问题描述:

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!

您好我解释一下我的问题。 我现在正在使用PHP而且我只是玩了一下现在我想知道我是否有一个文件例如: p>

  //这是file1.php \  n&lt;?php 
 $ text =“Hello”; 
?&gt; 
  code>  pre> 
 
 

并将其包含在file2中并包含file3: p> \ n

  //这是file2.php 
&lt;?php 
include'file1.php'; 
include'file3.php'; 
?&gt; 
  code>   pre> 
 
 

现在问我可以在file3中使用$ text吗?: p>

  //这是file3.php 
&lt;?php  
echo $ text; 
?&gt; 
  code>  pre> 
 
 

感谢您的回复! p> div>

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.