Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAdiv Abramson
6,919 PointsEcho and print_r do not accurately reflect strings
From the video examples, both echo and print_r seem to inaccurately display the keys and values that are strings.
$my_index_list = array("red", "green", "blue");
$state_capitals = array("California" => "Sacramento",
"Washington" => "Olympia",
"Wisconsin" => "Madison");
print_r($my_index_list);
print_r($state_capitals);
The output will be (I think): Array( [0] => red [1] => green [2] => blue )
Array ( [California] => Sacramento [Washington] => Olympia [Wisconsin] => Madison )
Why aren't the keys and values that are of string type not outputted with quote delimiters? It looks like their constants, just not capitalized according to convention.
I would expect something more like this:
Array( [0] => 'red' [1] => 'green' [2] => 'blue' )
Array ( ['California'] => 'Sacramento' ['Washington'] => 'Olympia' ['Wisconsin'] => 'Madison' )
If I include a reference to a string value from an array in an expression, will PHP supply the quotes to delimit the string value or do I need to provide them myself?
1 Answer
Scott Evans
4,236 PointsIf you are using your arrays values to access associative array indexes, you will not need to encapsulate the value within quotes. You are right in thinking PHP will do that for you.
the reason for echo
and print_r()
not showing the quotes is because they are purely for printing in human readable format. If you were to var_dump($state_capitals)
you would also see the data type and any coresponding encapsulation.
Hope this helps you understand