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

PHP output array-detailed explanation of printing array examples

黄舟
黄舟Original
2017-05-04 10:31:4718816browse

Suppose there is an array:

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

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 ] )

Please try to print

print_r($names);

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);

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)

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);
?>

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!

Statement:
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