How to use php variables from an included php file

喜欢而已 提交于 2019-12-29 08:57:11

问题


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?


回答1:


They should just roll over:

File1.php

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

File2.php

<?php
include("File1.php");
echo $var1; //Outputs TEST
?>



回答2:


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.




回答3:


When you include one file in another, everything is visible from both of them. Imagine them as having one file, so you use the variables the regular way - by typing their name out.



来源:https://stackoverflow.com/questions/5483759/how-to-use-php-variables-from-an-included-php-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!