Recent Posts

Showing posts with label Syntax. Show all posts
Showing posts with label Syntax. Show all posts

How to solve: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE

This error is most often encountered when attempting to reference an array value with a quoted key for interpolation inside a double-quoted string, when the entire complex variable construct is not enclosed in {}.

The error case:

This will result in Unexpected T_ENCAPSED_AND_WHITESPACE:
echo "This is a double-quoted string with a quoted array key in $array['key']";
//---------------------------------------------------------------------^^^^^

Possible fixes:

In a double-quoted string, PHP will permit array key strings to be used unquoted, and will not issue an E_NOTICE. So the above could be written as:
echo "This is a double-quoted string with an un-quoted array key in $array[key]";
//------------------------------------------------------------------------^^^^^
The entire complex array variable and key(s) can be enclosed in {}, in which case they should be quoted to avoid an E_NOTICEThe PHP documentation recommends this syntax for complex variables.
echo "This is a double-quoted string with a quoted array key in {$array['key']}";
//--------------------------------------------------------------^^^^^^^^^^^^^^^
// Or a complex array property of an object:
echo "This is a a double-quoted string with a complex {$object->property->array['key']}";
Of course, the alternative to any of the above is to concatenate the array variable in instead of interpolate it:
echo "This is a double-quoted string with an array variable " . $array['key'] . " concatenated inside.";
//----------------------------------------------------------^^^^^^^^^^^^^^^^^^^^^
For reference, see the section on Variable Parsing in the PHP Strings manual page

How to solve: Parse error: syntax error, unexpected T_VARIABLE

Possible scenario
I can't seem to find where my code has went wrong. Here is my full error:
Parse error: syntax error, unexpected T_VARIABLE on line x
what i am trying
$sql = 'SELECT * FROM dealer WHERE id="'$id.'"';
Answer
Parse error: A problem with the syntax of your program, such as leaving a semicolon off of the end of a statement or in like in above case missing . operator . The interpreter stops running your program when it encounters a parse error.
In simple word this is a syntax error, meaning that there is something in your code stopping it from being parsed correctly and therefore run.
What you should do is check carefully at the lines around where the error is for any simple mistakes
That error message means that in line x of the file, the PHP interpreter was expecting to see an open parenthesis but instead, it encountered something called T_VARIABLE. That T_VARIABLE thing is called a token. It's the PHP interpreter's way of expressing different fundamental parts of programs. When the interpreter reads in a program, it translates what you've written into a list of tokens. Wherever you put a variable in your program, there is aT_VARIABLE token in the interpreter's list.
so make sure you enable at least E_PARSE in your php.ini. Parse errors should not exist in production scripts.
i always recommended to while coding
error_reporting(E_ALL);
Also a good idea to use IDE which will let you know parse error while typing. You can sue
  1. NetBeans (fine peace of beauty, free)(imho its best)
  2. PhpStorm (Uncle Gordon love this :P , paid)
  3. Eclipse (beauty and the beast free)

How to solve: Parse error: syntax error, unexpected T_XXX

This happens when you have T_XXX token in unexpected place, unbalanced (superfluous) parentheses, use of short tag without activating it in php.ini, and many more.

How to solve: Parse error: syntax error, unexpected '['

This error comes in two variatians:

Variation 1

$arr = [1, 2, 3];
This array initializer syntax was only introduced in PHP 5.4; it will raise a parser error on versions before that. If possible, upgrade your installation or use the old syntax:
$arr = array(1, 2, 3);
See also this example from the manual.

Variation 2

$suffix = explode(',', 'foo,bar')[1];
Array dereferencing function results was also introduced in PHP 5.4. If it's not possible to upgrade you need to use a temporary variable:
$parts = explode(',', 'foo,bar');
$suffix = $parts[1];
See also this example from the manual.