在函数中使变量相等

时间:2014-12-23 10:40:06

标签: php arrays function var

我在语言文件中有一个函数,它存储一个文本数组。我还尝试创建一个变量来等于该数组中的特定值。

这是我的职能......

    function Day_1_Offer($day){
    static $offer = array(
        'SITE_OFFER' => 'TEXT1', //Offer text for front end
        'EMAIL_OFFER' => 'TEXT2' //Offer text for hidden form field
    );
    return $offer[$day];
}

这里我的var被分配了函数中的值...

$day1Txt = Day_1_Offer('EMAIL_OFFER');

我得到的错误是......

Fatal error: Call to undefined function Day_1_Offer() in

这是完整的language.php文件......

function Day_1_Offer($day){
    static $offer = array(
        'SITE_OFFER' => 'Text1', //Offer text for front end
        'EMAIL_OFFER' => 'Text2' //Offer text for hidden form field
    );
    return $offer[$day];
}

function Day_2_Offer($day){
    static $offer = array(
        'SITE_OFFER' => 'Text1', //Offer text for front end
        'EMAIL_OFFER' => 'Text2' //Offer text for hidden form field
    );
    return $offer[$day];
}

function Day_3_Offer($day){
    static $offer = array(
        'SITE_OFFER' => 'Text1', //Offer text for front end
        'EMAIL_OFFER' => 'Text2' //Offer text for hidden form field
    );
    return $offer[$day];
}

function Day_4_Offer($day){
    static $offer = array(
        'SITE_OFFER' => 'Text1', //Offer text for front end
        'EMAIL_OFFER' => 'Text2' //Offer text for hidden form field
    );
    return $offer[$day];
}

function Day_5_Offer($day){
    static $offer = array(
        'SITE_OFFER' => 'Text1', //Offer text for front end
        'EMAIL_OFFER' => 'Text2' //Offer text for hidden form field
    );
    return $offer[$day];
}

function Day_6_Offer($day){
    static $offer = array(
        'SITE_OFFER' => 'Text1', //Offer text for front end
        'EMAIL_OFFER' => 'Text2' //Offer text for hidden form field
    );
    return $offer[$day];
}

function Day_7_Offer($day){
    static $offer = array(
        'SITE_OFFER' => 'Text1', //Offer text for front end
        'EMAIL_OFFER' => 'Text2' //Offer text for hidden form field
    );
    return $offer[$day];
}

如果有人知道更有效的方法,那么很高兴看到它。

1 个答案:

答案 0 :(得分:0)

假设这是你的库文件,其中定义了fucntion

my_fun.php

function Day_1_Offer($day){
    static $offer = array(
        'SITE_OFFER' => 'TEXT1', //Offer text for front end
        'EMAIL_OFFER' => 'TEXT2' //Offer text for hidden form field
    );
    return $offer[$day];
}

这是您正在使用它的文件

的index.php

include 'my_fun.php';
$day1Txt = Day_1_Offer('EMAIL_OFFER');

您需要使用include来实现此目标