如果她在函数中创建,如何打印字符串


How print string if she created in function?

字符串$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;