I am trying to write a new line to a file with PHP and I'm getting the following error:
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. What is wrong with this line?
Example of lang_contents:
<?php
$lang['1234'] = 'Restaurants';
Example of new_contents:
<?php
$lang['1234'] = 'Restaurants';
$lang['1235'] = 'Transportation';
How to Fix It: If a syntax error appears, check to make sure that the parentheses are matched up correctly. If one end is missing or lined up incorrectly, then type in the correction and check to make sure that the code can be compiled. Keeping the code as organized as possible also helps.
Syntax errors are mistakes in the source code, such as spelling and punctuation errors, incorrect labels, and so on, which cause an error message to be generated by the compiler. These appear in a separate error window, with the error type and line number indicated so that it can be corrected in the edit window.
How To Fix parse error syntax error unexpected ' ' in wordpress Via FTP? In order to fix the Syntax Error in WordPress you need to edit the code that caused this error. The only possible way to resolve the syntax error is to directly exchange the faulty code via FTP or access the file you last edited using FTP.
The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.
If your trying to write $lang
as string to your file
$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
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).
What is your desired output?
If you desire output literally a $
followed by lang
characters, you have to escape $
:
$new_contents = $lang_contents."\n\$lang['".$line."'] = '".$value."';";
If you want output the content of $lang
variable followed by a [
you have to write:
$new_contents = $lang_contents."\n$lang"."['".$line."'] = '".$value."';";
Otherwise, if you want output the content of $lang[$line]
array item, you have to write:
$new_contents = $lang_contents."\n{$lang[$line]} = '".$value."';";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With