Parse error: syntax error, unexpected 'unset' (T_UNSET)

22,417

Solution 1

Try this, Reason for unset($linkExtHelp[0]) assigning to the variable echo $totalArray = You can't assign the unset() value to the variable, You can use to check before unset and after unset as like below. In other words, unset does not have any return value, since unset is a void. Void - does not provide a result value to its caller.

Syntax: void unset ( mixed $var [, mixed $... ] )

echo "Before unset: ".$linkExtHelp[0];
unset($linkExtHelp[0]);
$linkExtHelp = array_values($linkExtHelp);
echo "After unset: ".$linkExtHelp[0];

instead of

echo $totalArray  =     unset($linkExtHelp[0]);

Solution 2

unset does not return a value - it cannot be used meaningfully as an expression, even if such a production was accepted.

However, the parse error is caused because unset is a keyword and a "special production": even though unset looks like a function, it is not a function1. As such, unset is only valid as a statement2 per the language grammar.

The production can be found in zend_language_parser.y:

309 unticked_statement:
       |   ..
338    |   T_UNSET '(' unset_variables ')' ';'

1 The syntax is due to historical design choices, and arguably a mistake from a consistency viewpoint:

Note: Because [unset] is a language construct and not a function, it cannot be called using variable functions.

2 There is also "(unset) casting", but I'm ignoring that here.

Share:
22,417
Khandad Niazi
Author by

Khandad Niazi

Updated on September 09, 2020

Comments

  • Khandad Niazi
    Khandad Niazi over 3 years

    I am using simple php unset() function to remove and index of array but it's showing the following error:

    Parse error: syntax error, unexpected 'unset' (T_UNSET)
    

    Here is my erroneous code:

    echo $totalArray = unset($linkExtHelp[0]);
    

    Thanks in advance.

  • user2864740
    user2864740 about 10 years
    Nit: unset isn't a function. A real [void] function would have resulted in a different error.
  • Gem
    Gem almost 5 years
    May i know what is the error in this code : pastiebin.com/5d319d2506c52 i am getting same error like ( ! ) Parse error: syntax error, unexpected 'unset' (T_UNSET) in C:\wamp\www\voice_bank\playlist_action.php on line 11
  • Jason Aller
    Jason Aller over 3 years
    What does this add that the existing and accepted answers don't have?
  • MartinNajemi
    MartinNajemi over 3 years
    @JasonAller The existing answer will leave the array missing a first value, my answer re-indexes (reassigns keys for the values)