字符串$test1
、$test2
、$test3
、$test4
是在函数test()
中创建的。我怎么打印她?
conf.php
<?php
function test(){
$test1 = 'test1 string';
$test2 = 'test2 string';
$test3 = 'test3 string';
$test4 = 'test4 string';
}
?>
test.php
<?php
require_once("conf.php");
test();
echo $test1;
echo $test2;
echo $test3;
echo $test4;
?>
为什么代码不能工作?
为什么不打印$test1
、$test2
、$test3
和$test4
。
注::最好没有全局参数
您从未实际检索test()
函数的返回值,这应该可以工作:
conf.php
<?php
function test(){
$test='test string';
return $test;
}
?>
test.php
<?php
require_once("conf.php");
$test_value = test();
echo $test_value;
?>
还有,文本是require_once
而不是requare_once
。
对于必须使用数组的多个实体
所以,这里有一个通用的解决方案。
为了得到确定的答案,我们需要一个确定的问题
function test() {
$test[] = 'test1 string';
$test[] = 'test2 string';
$test[] = 'test3 string';
$test[] = 'test4 string';
return $test;
}
require_once "conf.php";
$test = test();
echo $test[0];
echo $test[1];
echo $test[2];
echo $test[3];
这样检查:
$test=test();
echo $test;