1

Why do I need to do array indexing using curly braces inside an if statement expression? Why is the following illegal?

$birthday = "1990-01-18";
$date_birth = explode("-", $birthday);
if ($date_birth[1] != "00" && $date_birth[2] != "00") {
    $monthName = date('F', mktime(0, 0, 0, $date_birth[1], 10));
    echo "$monthName $date_birth[2]";
}

However, the following works fine:

$birthday = "1990-01-18";
$date_birth = explode("-", $birthday);
if ($date_birth{1} != "00" && $date_birth{2} != "00") {
    $monthName = date('F', mktime(0, 0, 0, $date_birth[1], 10));
    echo "$monthName $date_birth[2]";
}
Matteo Tassinari
  • 16,776
  • 5
  • 55
  • 77
  • Both seem to work http://sandbox.onlinephpfunctions.com/code/7228bf53485ff759fa17d61ca900768d74a9a162 and the first version is the "proper" one. – Matteo Tassinari Apr 05 '16 at 21:13

1 Answers1

1

The two versions are exactly equivalent, as shown here.

Also, the php manual page on arrays states that:

Both square brackets and curly braces can be used interchangeably for accessing array elements (e.g. $array[42] and $array{42} will both do the same thing [...])

Matteo Tassinari
  • 16,776
  • 5
  • 55
  • 77