如何在php中从另一个文件添加变量

时间:2018-08-07 13:27:45

标签: php

也许我没有做正确的问题,或者我不是很清楚或准确地表达自己的意思...

下一个问题是

file1.php

......
require_once "locales/" . $_SESSION['lang'] . ".php";
......

语言环境/en.php

<?php
$lang = array(
"text1" => "Access",
        );
?>

file2.php

......
include ('file1.php');
......
// User is not logged in, display one of the forms
private function form($error,$formname) {
    // Throttle failed attempts
    if($formname == 'signin' && $error != 0)
        sleep(1);

    // Show a sign up or sign in link in the navigation
    if($formname == 'signin')
        $link = '<p><a href="?form=signup">Create new account</a></p>';
    else
        $link = '<p><a href="'.$this->clean_uri().'">Access</a></p>';

......

因此,我需要将“ Access”一词更改为从en.php文件中插入该词的变量,但是尝试了多种语法后,我无法成功,怎么办呢?

我尝试过的事情:

$link = '<p><a href="'.$this->clean_uri().'">'.$lang['text1'].'</a></p>';


$link = '<p><a href="'.$this->clean_uri().'">'.$lang["text1"].'</a></p>';


$link = '<p><a href="'.$this->clean_uri().'">'.$lang[text1].'</a></p>';

还有更多...

[解决方案] 我终于找到了解决方法:

在file2.php中,在包含行之后,我以这种方式恢复了变量:

$_POST['text1'] = $lang['text1'];

然后,在函数内部,我像这样恢复它:

// User is not logged in, display one of the forms
    private function form($error,$formname) {
        // Throttle failed attempts
        if($formname == 'signin' && $error != 0)
            sleep(1);

    // Show a sign up or sign in link in the navigation
    if($formname == 'signin')
        $link = '<p><a href="?form=signup">Create new account</a></p>';
    else
        $link = '<p><a href="'.$this->clean_uri().'">" .$_POST['text1']. "</a></p>';

0 个答案:

没有答案