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

时间:2013-01-17 15:13:31

标签: php

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

P.S。:最好没有全局参数。

3 个答案:

答案 0 :(得分:1)

您实际上从未检索过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

答案 1 :(得分:0)

检查这个方式:

$test=test();
echo $test;

答案 2 :(得分:0)

对于您必须使用数组的多个实体 所以,这是一个通用的解决方案 对于某些答案,我们需要一个问题

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];