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]";
}
1

1 Answer 1

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 [...])

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.