PHP is a widely used web programming language that supports multiple data types, of which arrays are one of the most important. In actual development, we often need to output the contents of an array. This article will introduce in detail how PHP echo outputs an array.
1. Use the print_r() function to output an array
The print_r() function in PHP can print the contents of variables, including arrays, in an easy-to-read manner. The specific usage is as follows:
$array = array('foo' => 'bar', 'one' => 'two', 'three' => 'four'); print_r($array);
Output result:
Array ( [foo] => bar [one] => two [three] => four )
In this example, we first define an associative array $array, and then use the print_r() function to print it to the screen.
The print_r() function will format and output the contents of the array by default, with each line indented by a tab character. This output method is very suitable for debugging the program, but if we want to output the contents of the array directly to the HTML page, we need to process the text output by the print_r() function.
The following is an example of outputting formatted array contents through echo:
$array = array('foo' => 'bar', 'one' => 'two', 'three' => 'four'); echo '<pre class="brush:php;toolbar:false">'; echo print_r($array, true); echo '';
In this example, we set the second parameter of print_r() to true, which will make the function Returns a string instead of outputting text directly. Then use echo to output a string with the
tag, so that the browser will display the output in a pre-formatted form, making the array more readable. </p> <p>2. Use the var_dump() function to output an array</p> <p>Unlike print_r(), the var_dump() function can print out detailed information such as the type, length, and value of the variable. It is very suitable for debugging programs. . The specific usage is as follows: </p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">$array = array('foo' => 'bar', 'one' => 'two', 'three' => 'four'); var_dump($array);
Output results:
array(3) { ["foo"]=> string(3) "bar" ["one"]=> string(3) "two" ["three"]=> string(4) "four" }
In this example, we use the var_dump() function to print the array $array to the screen.
var_dump() function will output the detailed information of the variable according to a certain format, including the number of elements, element type, element key value, etc. If the variable is an array, each element will contain a key name and a key value. This information can help programmers better understand the array structure and contents.
Similar to print_r(), if we want to output the output of var_dump() directly to the page, we can use the
tag to better display the output content. </p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">$array = array('foo' => 'bar', 'one' => 'two', 'three' => 'four'); echo '<pre class="brush:php;toolbar:false">'; var_dump($array); echo '';
3. Use foreach() to loop out array elements
In actual development, we often need to output the array content in our own way. In this case, you can use PHP's foreach() loop to iterate over the array elements and output them in an appropriate manner. The specific code is as follows:
$array = array('foo' => 'bar', 'one' => 'two', 'three' => 'four'); foreach ($array as $key => $value) { echo $key . ' = ' . $value . '<br/>'; }
Output result:
foo = bar one = two three = four
In this example, we use foreach() to loop through the array elements and output each element in the form of "key = value" to the screen. It should be noted that we use a newline character
in the echo statement, which will cause each element to occupy its own line on the screen to facilitate user viewing.
Summary
The above are several ways of how PHP echo outputs arrays. No matter which method is used, it has its applicable scenarios, advantages and disadvantages. In actual development, programmers need to use these methods flexibly to better meet business needs.
The above is the detailed content of How to output array in php echo. For more information, please follow other related articles on the PHP Chinese website!