Why does "isset($a[0]) and unset($a[0]);" cause syntax error?

251 views Asked by At

My Code:

$a = [];
isset($a[0]) and unset($a[0]);

it shows "syntax error, unexpected 'unset' (T_UNSET)"

but

$a = [];
isset($a[0]) and exit();

it works!

Both of exit() and unset() are returning no value. Why does one work but not the other?

2

There are 2 answers

1
Umair Ayub On

As I asked in comments and you said you want to delete a value from Array,

Why not simply write

$a = [];
if(isset($a[0])){
unset($a[0]);
// And exit() if you want to
}
0
Austin On

unset is a language construct, not a real function (this is why you get T_UNSET and not a more generic term), so it doesn't play by the same rules as a normal function would. isset and exit are also language constructs, but they behave more like normal functions.