Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting a syntax error in PHP?

Tags:

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';
like image 925
ShoeLace1291 Avatar asked Feb 27 '16 01:02

ShoeLace1291


People also ask

How do you fix a syntax error?

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.

What triggers a syntax error?

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 do I fix parse error syntax error unexpected?

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.

What is syntax error unexpected?

The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.


2 Answers

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

like image 184
roullie Avatar answered Oct 03 '22 05:10

roullie


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."';";
like image 32
fusion3k Avatar answered Oct 03 '22 05:10

fusion3k