为什么我在PHP中遇到语法错误?

Sho*_*291 6 php

我试图用PHP写一个新行到一个文件,我收到以下错误:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

public function add_line($line, $value, $file){

        $CI =& get_instance();
        $CI->load->helper('file');

        foreach($this->existing_langs as $lang){

            $lang_contents = read_file($this->lang_path.'/'.$lang.'/'.$file.'_lang.php');

            $new_contents = $lang_contents."\n$lang['".$line."'] = '".$value."';"; //Error happens on this line

            write_file($this->lang_path.'/'.$lang.'/'.$file.'_lang.php', $new_contents, 'w+');

        }

    }
Run Code Online (Sandbox Code Playgroud)

我已经用php注释指出了错误发生的那一行.这条线有什么问题?

lang_contents的示例:

<?php
$lang['1234'] = 'Restaurants';
Run Code Online (Sandbox Code Playgroud)

new_contents的示例:

<?php
$lang['1234'] = 'Restaurants';
$lang['1235'] = 'Transportation';
Run Code Online (Sandbox Code Playgroud)

rou*_*lie 7

如果您尝试将$lang字符串写入文件

$lang_contents."\n".'$lang'."['".$line."'] = '".$value."';";
Run Code Online (Sandbox Code Playgroud)

封闭$lang"将只访问您的$lang是或不是阵列.因为您$lang在文件路径中使用.我假设它不是一个数组.因此,使用..."\n$lang['".$line."']...将仅$lang使用索引调用$line