phpparse-error

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!!!


Solution

  • 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