0

I'm having problems formatting arrays and nested arrays with print function in PHP.

So far into learning PHP, I have been wrapping print commands with " " and putting basic html like <p> within the quotes with the single variables, and everything works fine. Today I have been creating arrays and nested arrays and have been getting some strange behaviour.

For instance:

// nested array
$array2 = array(6, "fox", "dog", array("x", "y", "z"));
// display nested array index 1
print "<p> $array2[3][1] </p>";

This prints Array[1] instead of y.

If I do this it prints ok:

print "<p>" . $array2[3][1] . "</p>";

The last array I am trying to get to print using a print readible with <pre> tags. So far I have tried this, but all I see in the browser is just array

print_r("<pre>" . $array2 . "</pre>");

I must be doing something wrong if the tags will not concat?

2
  • You have to convert an array to string somehow. Check out the PHP's implode function php.net/manual/en/function.implode.php Commented Oct 21, 2014 at 15:40
  • "<p> {$array2[3][1]} </p>" Commented Oct 21, 2014 at 15:43

2 Answers 2

1

When you use

print_r("<pre>" . $array2 . "</pre>");

You're concatenating the strings <pre> and </pre> with an array, it automatically converts the array to "Array", and it equal to the following:

print_r("<pre>" . (string)$array2 . "</pre>");

What you actually want to do is use print_r on the array itself - not turn it into a string. You can take use of the second parameter in print_r to return the value rather than outputting it:

echo '<pre>' . print_r($array2, true) . '</pre>';
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks - I noticed your using single quotes, does this matter in this case? The only time I have used single quotes is in the $_POST function so far. Also, are echo and print the same?
How are echo and print different in PHP?. Essentially, echo is faster, but reacts a little differently (you probably won't notice the difference, so just use echo. When using single quotes variables inside do not expand (variable expanding is why echo "<strong>$test</strong>"; works. Use whichever you prefer as the speed difference is so minimal that it does not matter.
I see, thanks. So when you say "expand", what would happen if you wrote echo '<strong>$test</strong>' would this still be expanded, but will execute faster?
@user1574598 No - single quotes don't expand. $test = 'abc'; echo "<strong>$test</strong>"; would output <strong>123</strong>. $test = 'abc'; echo '<strong>$test</strong>'; would output <strong>$test</strong>. Don't bother about which is faster - it's micro optimization and is not needed. Bother about what you find easiest to read.
1

You should add curly brackets:

// nested array
$array2 = array(6, "fox", "dog", array("x", "y", "z"));
// display nested array index 1
print "<p> {$array2[3][1]} </p>";

From the manual:

If a dollar sign ($) is encountered, the parser will greedily take as many tokens as possible to form a valid variable name. Enclose the variable name in curly braces to explicitly specify the end of the name.

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.