dongyinpan9284
2016-11-25 13:16
浏览 171
已采纳

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

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!

图片转代码服务由CSDN问答提供 功能建议

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

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

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

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

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

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

感谢您的回复!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doudao8283 2016-11-25 13:19
    已采纳

    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.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题