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?

i alarmed alien
  • 9,412
  • 3
  • 27
  • 40
user1574598
  • 3,771
  • 7
  • 44
  • 67

2 Answers2

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>';
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
  • 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? – user1574598 Oct 21 '14 at 16:05
  • [How are echo and print different in PHP?](http://stackoverflow.com/questions/234241/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 "$test";` works. Use whichever you prefer as the [speed difference is so minimal that it does not matter](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php). – h2ooooooo Oct 21 '14 at 16:09
  • I see, thanks. So when you say "expand", what would happen if you wrote `echo '$test'` would this still be expanded, but will execute faster? – user1574598 Oct 21 '14 at 16:23
  • @user1574598 No - single quotes don't expand. `$test = 'abc'; echo "$test";` would output `123`. `$test = 'abc'; echo '$test';` would output `$test`. Don't bother about which is faster - it's micro optimization and is not needed. Bother about what you find easiest to read. – h2ooooooo Oct 21 '14 at 16:23
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.

Pedro Amaral Couto
  • 2,056
  • 1
  • 13
  • 15