0
for($j = 0; $j < $rows; ++$j) 
{
    $result->data_seek($j);
    $row = $result->fetch_array(MYSQLI_NUM);
echo <<<_END
    <pre>
    Author: $row[0]
    Title: $row[1]
    Type: $row[2]
    Year: $row[3]
    ISBN: $row[4]
    </pre>
_END;
}

There are two things I've encountered in using echo for block of texts and I'm a bit stumped in coming up with an explanation.

  1. Any indentation of the echo block will cause the webpage to crash.

  2. If I change the fetch_array type to either MYSQLI_ASSOC, or MYSQLI_BOTH, associative calls such as $row['author'] cause the page to crash. Whereas using single-line calls to echo while accessing $row are working fine.

1
  • 3
    The indentation only pertains the closing heredoc marker _END;. And string interpolation in double quoted / heredoc context either needs literal "$row[key]" or complex "${row['key']}" syntax for quoted keys. Commented Aug 31, 2015 at 15:09

1 Answer 1

4
  1. PHP heredoc's closing identifier should not indented. Here is warning paragraph from the manual.

Warning It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system.

  1. Inside heredoc, array keys either should not be quoted, or quoted inside ${} notation.

    $array[key] // works (the only place you do not quote string keys in PHP)
    ${array["key"]} // works
    $array["key"] // doesn't work
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.