Home > Backend Development > PHP Tutorial > PHP output array-detailed explanation of printing array examples

PHP output array-detailed explanation of printing array examples

黄舟
Release: 2023-03-07 11:22:01
Original
18864 people have browsed it

Suppose there is an array:

<?php
$arr=["PHP","中文","网"];
echo $arr;
?>
Copy after login

What will the above code output? You can try the above results locally.

We generally use print_r to print arrays (of course you can also use var_dump, but the structure is not clear)

bool print_r ( mixed $expression [, bool $return ] )
Copy after login

Please try to print

print_r($names);
Copy after login

when the second parameter is true When, print_r will not print the array directly, but will return the printed content as a string

echo print_r($names, true);
Copy after login

We can use echo to print a string, integer, or floating point type, but we cannot use it to print array.

An array is composed of a series of elements. If you want to print, then each element should be printed, not the entire array.

Outputting array elements in PHP can be achieved through output statements, such as echo statements, print statements, etc. However, this output method can only output a certain element in the array, while the array structure can be output through the print_r() function.

The syntax format is as follows:

bool print_r(mixed expression)
Copy after login

If the parameter expression of the function is an ordinary integer, string or real variable, the variable itself will be output. If the parameter is an array, all elements in the array are output in order of key value and element.

Example of print_r() function output array:

<?php
header("Content-Type:text/html; charset=utf-8");
$array = array(1=>"PHP",2=>"中文",3=>"网");
print_r($array);
?>
Copy after login

The output result is:

PHP output array-detailed explanation of printing array examples

【Recommended tutorials】

1. Recommended related topics: "php array (Array)"

The above is the detailed content of PHP output array-detailed explanation of printing array examples. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template