ADVERTISEMENTS

How to display array structure and values in PHP

PHP provides a variety of built-in functions for debugging code execution. For display array structure with values in PHP, we use print_r() method which is used to display the contents of a variable in a human-readable format. It takes a variable as an argument and returns a string representation of the variable's contents.

PHP provides one another way for rending array is var_dump(). It takes one or more variables as an argument and returns detailed information about the variable, such as its data type, structure and size.

The following example will explain you about array rendering ways.

<?php
$etutsArray = array("apple", "banana", "cherry");
print_r($etutsArray);
?>

It's important to note that, if you want to print the output of print_r() function directly to the browser, you should use echo function or wrap it in pre tag, otherwise it will be difficult to read.

PHP (Hypertext Preprocessor) is a server-side scripting language used to create web pages and web applications. It can be embedded into HTML and is often used in combination with databases such as MySQL to create dynamic, interactive websites. You can learn php from our PHP Tutorials and PHP Examples

ADVERTISEMENTS