From the course: PHP for Web Designers

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Inspecting an array's elements

Inspecting an array's elements - PHP Tutorial

From the course: PHP for Web Designers

Start my 1-month free trial

Inspecting an array's elements

Arrays are widely used in PHP. The input from a form is stored as an array. At each row of a database search result is an array. In the file that I've got open here, the array elements are hard-coded above the doc type, but you often don't know in advance what an array contains or how it's structured. Thankfully PHP has a built in function that let's you examine the contents and structure of an array. Its an essential tool used in testing and it helps explain how PHP sees arrays internally making it easier for you to understand how to get the information you want. To display the contents of an array you pass the array variable as an argument to a function called print_r. And so what I'm going to do is in the body of this page, I'm going to add a couple of pre tags. Adding the pre tags makes it easier to see the output of print_r in a browser. And between those pre tags I'm going to add my PHP block and then, between there, will be print_r, and the argument will be the name of the…

Contents