Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

syntax

php

unset

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.

like image 291
Khandad Niazi Avatar asked Jan 30 '14 06:01

Khandad Niazi


2 Answers

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.

like image 67
user2864740 Avatar answered Nov 17 '22 09:11

user2864740


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]);
like image 43
Krish R Avatar answered Nov 17 '22 08:11

Krish R