简体   繁体   中英

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:

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';

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

It's possible that the strings are trying to pick up $lang as a value. 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).

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."';";

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."';";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM