Why am I getting a syntax error in PHP?

I try to write a new line to a file with PHP and I get 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 pointed to the line where the error occurs with the php comment. What is wrong with this line?

Example lang_contents:

 <?php $lang['1234'] = 'Restaurants'; 

New_contents example:

 <?php $lang['1234'] = 'Restaurants'; $lang['1235'] = 'Transportation'; 
+6
source share
5 answers

If you are trying to write $lang as a string to your file

 $lang_contents."\n".'$lang'."['".$line."'] = '".$value."';"; 

spanning $lang with " will just gain access to your $lang , which is or not an array. since you are using $lang in your file path. I assume it is not an array. Therefore, using ..."\n$lang['".$line."']... just call $lang with index $line

+7
source

It is possible that the lines are trying to take $ lang as a value. Double quotes allow 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 it, do it.

 $new_contents = "$lang_contents\n\$lang['$line'] = '$value';"; 
+4
source

The double-quoted string is evaluated: in your code, php tries to evaluate $lang[ , but this throws an error because php expects $lang (variable) or $lang[n] (array).

What is your desired result?

If you want to get literally $ followed by lang characters, you need to exit $ :

 $new_contents = $lang_contents."\n\$lang['".$line."'] = '".$value."';"; 

If you want to display the contents of the $lang variable, followed by [ , you need to write:

 $new_contents = $lang_contents."\n$lang"."['".$line."'] = '".$value."';"; 

Otherwise, if you want to display the contents of the element of the array $lang[$line] , you need to write:

 $new_contents = $lang_contents."\n{$lang[$line]} = '".$value."';"; 
+4
source

try it

 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+'); } } 
+3
source

This will have the value of the $lang variable.

 $new_contents = $lang_contents."\n" . $lang . "['".$line."'] = '".$value."';"; 
+3
source

All Articles