[英]Why am I getting a syntax error in PHP?
I am trying to write a new line to a file with PHP and I'm getting the following error: 我试图用PHP写一个新行到一个文件,我收到以下错误:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
This is my code: 这是我的代码:
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+');
}
}
I have pointed out the line the error occurs on with a php comment. 我已经用php注释指出了错误发生的那一行。 What is wrong with this line? 这条线有什么问题?
Example of lang_contents: lang_contents的示例:
<?php
$lang['1234'] = 'Restaurants';
Example of new_contents: new_contents的示例:
<?php
$lang['1234'] = 'Restaurants';
$lang['1235'] = 'Transportation';
If your trying to write $lang
as string to your file 如果您尝试将$lang
作为字符串写入您的文件
$lang_contents."\n".'$lang'."['".$line."'] = '".$value."';";
enclosing $lang
with "
will just access your $lang
which is or not an array. since your using $lang
in your file path. I assume it's not an array. Hence using ..."\\n$lang['".$line."']...
will just call $lang
with an index of $line
封闭$lang
, "
只会访问你的$lang
,它是否是一个数组。因为你在文件路径中使用$lang
。我认为它不是数组。因此使用..."\\n$lang['".$line."']...
只会用$line
的索引调用$lang
It's possible that the strings are trying to pick up $lang as a value. 字符串可能会尝试将$ lang作为值。 Double quotes allow for variables to have the value passed there. 双引号允许变量具有在那里传递的值。 use single quotes. 使用单引号。 Try this and see what happens. 试试这个,看看会发生什么。
try this line of code 试试这行代码
$new_contents = $lang_contents . "\n".'$lang[\'' . $line . '\'] = \'' . $value . '\';';
EDIT: For the cleanest way to write this, do this 编辑:为了最清楚地写这个,做这个
$new_contents = "$lang_contents\n\$lang['$line'] = '$value';";
String in double quotes are evaluated: in your code, php try to evaluate $lang[
but this generate an error because php expects $lang
(a variable) or $lang[n]
(an array). 评估双引号中的字符串:在你的代码中,php尝试评估$lang[
但这会产生错误,因为php期望$lang
(变量)或$lang[n]
(数组)。
What is your desired output? 你想要的产量是多少?
If you desire output literally a $
followed by lang
characters, you have to escape $
: 如果你希望输出字面上是$
后跟lang
字符,你必须逃避$
:
$new_contents = $lang_contents."\n\$lang['".$line."'] = '".$value."';";
If you want output the content of $lang
variable followed by a [
you have to write: 如果你想输出$lang
变量的内容后跟一个[
你必须写:
$new_contents = $lang_contents."\n$lang"."['".$line."'] = '".$value."';";
Otherwise, if you want output the content of $lang[$line]
array item, you have to write: 否则,如果要输出$lang[$line]
数组项的内容,则必须写:
$new_contents = $lang_contents."\n{$lang[$line]} = '".$value."';";
try this 尝试这个
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+');
}
}
这将具有变量$lang
的值。
$new_contents = $lang_contents."\n" . $lang . "['".$line."'] = '".$value."';";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.