How to Echo or Print an Array in PHP

Avatar

By squashlabs, Last Updated: Nov. 21, 2023

How to Echo or Print an Array in PHP

To echo or print an array in PHP, you can use the print_r() or var_dump() functions. These functions allow you to display the contents of an array in a human-readable format. Here are the steps to echo or print an array in PHP:

Using the print_r() function

The print_r() function is a built-in PHP function that prints the information about a variable in a more readable format. It is commonly used for debugging purposes and to display the contents of an array. Here's how you can use the print_r() function to echo an array:

$array = array('apple', 'banana', 'orange');
print_r($array);

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)

In the above example, the print_r() function is used to display the contents of the $array variable. The output shows each element of the array along with its corresponding index.

Related Article: How to Use the Now Function in PHP

Using the var_dump() function

The var_dump() function is another built-in PHP function that is used to display information about a variable, including its type and value. It is particularly useful for debugging and understanding the structure of complex variables like arrays. Here's how you can use the var_dump() function to echo an array:

$array = array('apple', 'banana', 'orange');
var_dump($array);

Output:

array(3) {
  [0]=>
  string(5) "apple"
  [1]=>
  string(6) "banana"
  [2]=>
  string(6) "orange"
}

In the above example, the var_dump() function is used to display information about the $array variable. The output includes the type of the variable, the number of elements in the array, and the value of each element.

Best Practices

Related Article: How to Delete An Element From An Array In PHP

- When using print_r() or var_dump(), it's a good practice to wrap the output in <pre></pre> tags. This preserves the formatting and makes the output more readable. For example:

$array = array('apple', 'banana', 'orange');
echo '<pre>';
print_r($array);
echo '</pre>';

- If you want to capture the output of print_r() or var_dump() in a string variable instead of directly echoing it, you can use the ob_start() and ob_get_clean() functions. Here's an example:

$array = array('apple', 'banana', 'orange');
ob_start();
print_r($array);
$output = ob_get_clean();
echo $output;

In the above example, the ob_start() function starts output buffering, capturing all subsequent output. The print_r() function is then called to print the array, and the output is stored in the buffer. Finally, the ob_get_clean() function retrieves the buffer content and assigns it to the $output variable, which can be echoed or used as needed.

You May Also Like

How to Use Laravel Folio: Tutorial with Advanced Examples

Guide to using Laravel Folio with practical examples and advanced use cases in PHP. This article covers the installation and setup of Laravel Folio, … read more

PHP vs Java: A Practical Comparison

Evaluating the differences between PHP and Java can be overwhelming, but this article provides a practical comparison to help you make an informed de… read more

Processing MySQL Queries in PHP: A Detailed Guide

Learn how MySQL queries are returned in PHP and the steps involved in this process. Understand the syntax, retrieve and access the result set, displa… read more

How to Fix Error 419 Page Expired in Laravel Post Request

Troubleshoot and resolve Laravel error 419, commonly associated with session expiration. Verify CSRF token, check session configuration, clear browse… read more

How To Check If A PHP String Contains A Specific Word

Table of Contents 1. Using the strpos() Function2. Using the preg_match() FunctionWhy is this question asked?Alternative Ideas and SuggestionsBest P… read more

How to Work with PHP Arrays: a Complete Guide

Learn how to work with arrays in PHP, from basic concepts to advanced techniques. This complete guide covers everything you need to know, including s… read more

PHP Date and Time Tutorial

Learn how to work with date and time in PHP with this tutorial. From establishing the basics of date and time functions to manipulating time and date… read more

Optimizing Laravel Apps: Deployment and Scaling Techniques

In this article, we will explore techniques for optimizing Laravel apps in production environments. We will cover deployment strategies using Envoy, … read more

How to Use Loops in PHP

Loops are an essential part of PHP programming. They allow you to iterate through data and perform repetitive tasks more efficiently. In this article… read more

How To Convert A Php Object To An Associative Array

Table of Contents Method 1: TypecastingMethod 2: JSON Encoding and DecodingMethod 3: Iterating over Object Properties Converting a PHP object to an … read more