How do I print an array in PHP?

To print or echo an array in PHP, you can use the print_r(variable, return) or var_dump(variable1, variable2, ...) functions. The print_r() function prints information about the passed variable in human-readable form. The first parameter is the "variable" we want to get information about. The second parameter is optional and determines whether the function should print or just return the result. The default value is "False". If the parameter value is "True", the function will return the value as if it were printed to the output stream. The var_dump() function takes one argument and displays structured information about the variable, including the type and value. You can also use the foreach(array as value) loop, which only works with arrays and iterates over each element in the array. In this Print PHP Array example, we use the print_r() function to print information about a given array. Below you can see more examples of PHP array printouts with a detailed description of each method. Click Execute to run the PHP Print Array Example online and see the result.
Loading...