5

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?

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
yrssoft
  • 79
  • 7

2 Answers2

3

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.

Austin
  • 2,982
  • 2
  • 28
  • 36
0

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
}
Umair Ayub
  • 19,358
  • 14
  • 72
  • 146
  • Because this is simple way to write and read ... else, future programmers working on your code will say bad words about you :P – Umair Ayub Jun 18 '15 at 08:14