What is unexpected T_VARIABLE in PHP? - php

I get this PHP error:
Parse error: syntax error, unexpected
T_VARIABLE
From this line:
$list[$i][$docinfo['attrs']['#groupby']] = $docinfo['attrs']['#count'];
Is there anything wrong with this line?

There might be a semicolon or bracket missing a line before your pasted line.
It seems fine to me; every string is allowed as an array index.

It could be some other line as well. PHP is not always that exact.
Probably you are just missing a semicolon on previous line.
How to reproduce this error, put this in a file called a.php:
<?php
$a = 5
$b = 7; // Error happens here.
print $b;
?>
Run it:
eric#dev ~ $ php a.php
PHP Parse error: syntax error, unexpected T_VARIABLE in
/home/el/code/a.php on line 3
Explanation:
The PHP parser converts your program to a series of tokens. A T_VARIABLE is a Token of type VARIABLE. When the parser processes tokens, it tries to make sense of them, and throws errors if it receives a variable where none is allowed.
In the simple case above with variable $b, the parser tried to process this:
$a = 5 $b = 7;
The PHP parser looks at the $b after the 5 and says "that is unexpected".

In my case it was an issue of the PHP version.
The .phar file I was using was not compatible with PHP 5.3.9. Switching interpreter to PHP 7 did fix it.

Related

How to fix Syntax error, unexpected '' (T_STRING)?

The error that I am receiving is this: Parse error: syntax error, unexpected 'TokenType' (T_STRING) in C:\Program Files\Ampps\www\cs5339\lepichardo\4339_f22_assignment1\TokenType.php on line 2. To be clear, both files are php, and they both are in the same director. The code below is where I include the enum TokenType so that I would be able to use it.
<?php
include 'TokenType.php'
?>
The TokenType.php looks like this
<?php
enum TokenType{
case INT;
case STRING;
case COMMA;
}
?>
As far as I now, this should be fine, but gives me that error in that particular line.
If there is any other way to declare and imlpement the enum variable in php I would like to be explained to how to. Thanks in advance!!!
enums are only available in PHP 8.1+. The error suggests you are running PHP 7.
Here's some results of running the following code using various PHP versions (using https://3v4l.org/qHU4f)
<?php
enum TokenType{
case INT;
case STRING;
case COMMA;
}
echo 'hello world';
Output for 8.1.0 - 8.1.11, 8.2rc1 - rc3
hello world
Output for 8.0.1 - 8.0.24
Parse error: syntax error, unexpected identifier "TokenType" in /in/qHU4f on line 2
Process exited with code 255.
Output for 7.4.0 - 7.4.32
Parse error: syntax error, unexpected 'TokenType' (T_STRING) in /in/qHU4f on line 2
Process exited with code 255.
As you can see by the last result, unexpected 'TokenType' (T_STRING) is an error you would receive in PHP 7

Getting the Scope Resolution Operator example from php.net to run on PHP 5.2.17

I'm just discovering scope resolution operators in PHP for the first time so I am trying to understand how they work. I viewed example #1 on the php.net page, and have insured I'm on PHP5.2+, however when I run the code:
<?php
class MyClass {
const CONST_VALUE = 'A constant value';
}
$classname = 'MyClass';
echo $classname::CONST_VALUE; // As of PHP 5.3.0
echo MyClass::CONST_VALUE;
?>
However, I get the following error:
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM,
expecting ',' or ';' in
/var/www/temp/scope.php on
line 7
Didn't realise that the code only worked on PHP5.3+, I upgraded and things are working fine.

getting "expecting T_PAAMAYIM_NEKUDOTAYIM" when working with constants

i have the following piece of code in php:
define('QUERY', $some_query_string);
if(empty(QUERY) || mb_strlen(QUERY) < 4):
//worn the user about incorrect query input
endif;
when running this script i'm getting the fallowing error Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM, but when i'm using an ordinary variable instead of constant everything works just fine!
The usage of constant is not required, but i would appreciate if anyone could explain why i'm getting that error?
(i'm newby here, so please tell me if i have to provide more details)
empty only works with variables. If you want to check whether a constant has been defined, use defined. The error messages invalid uses of empty produce aren't necessarily very intuitive, the message about T_PAAMAYIM_NEKUDOTAYIM mostly means the parser tripped up.

Why is this a parse error?

The following code
$t = 1 – (1 - 2);
yields:
Parse error: syntax error, unexpected T_STRING on line 3 in php 5.2.3
and in 5.4 I get Parse error: in <file> on line 3
To me it seems like I'm subtracting one expression from another, which I would imagine is legal. Why is this a parse error?
The first "–" is an en-dash, but it should be a hyphen-minus like the latter one: "-". Replace it, and your code will work.

PHP error when attempting to print variables within an echo using heredoc

When I try to do this:
var $example = "Example";
echo <<<EOT
<p>$example</p>
EOT;
I get this error:
Parse error: syntax error, unexpected
T_VAR in ..... on line ...
What is going on here?? To my knowledge this should work.
I'm using PHP 5.3.5.
The var keyword on the first line is for declaring variables in classes only. Leave it out.
remove the word var.
see http://www.php.net/manual/en/language.variables.basics.php
There is no keyword var in PHP. Not in PHP5 anyway - it's only accepted due to backward compatibility, and is used to define class variables.
D'oh. Removing the 'var' keyword fixed it. Thanks for the input guys!
Unfortunately however it didn't solve my actual problem. See here:
$param = array_merge($_REQUEST, $_FILES, $_COOKIE);
$param['example'] = "example";
example();
function example()
{
global $param;
echo <<<EOT
<p>$param['example']</p>
EOT;
return;
}
This time the complaint is:
Parse error: syntax error, unexpected
T_ENCAPSED_AND_WHITESPACE, expecting
T_STRING or T_VARIABLE or T_NUM_STRING
in ..... on line ...
Again, what is going on here?

Categories