PHP 文件包含和函数调用示例 - test_2.php, test3_2_1.php, test3_1.php
PHP 文件包含和函数调用示例 - test_2.php, test3_2_1.php, test3_1.php
本示例演示了如何在 PHP 中使用 require_once 引入文件,并在不同文件中调用同一文件中的函数。
1. 创建文件 test3_1.php
<?php
function fun1(){
echo 'this is function fun1 in test3_1.php<br>';
}
function fun2(){
echo 'this is function fun2 in test3_1.php<br>';
}
?>
2. 创建文件 test_2.php
<?php
require_once 'test3_1.php';
fun1();
fun2();
?>
3. 创建文件 test3_2_1.php
<?php
require_once 'test3_1.php';
fun1();
fun2();
?>
代码说明:
test3_1.php定义了两个函数fun1()和fun2()。test_2.php和test3_2_1.php使用require_once 'test3_1.php';语句引入test3_1.php文件。- 然后分别调用
test3_1.php文件中定义的两个函数fun1()和fun2()。
运行结果:
运行 test_2.php 和 test3_2_1.php 文件,都会输出以下内容:
this is function fun1 in test3_1.php<br>
this is function fun2 in test3_1.php<br>
总结:
本示例演示了如何通过 require_once 在 PHP 文件之间进行包含,以及如何在一个文件中调用其他文件定义的函数。这种方式可以有效地组织代码,提高代码的可重用性。
原文地址: https://www.cveoy.top/t/topic/jWX0 著作权归作者所有。请勿转载和采集!