Home >Backend Development >PHP Problem >What is the difference between print_r and print

What is the difference between print_r and print

王林
王林Original
2020-06-29 15:58:395714browse

The difference between print_r and print is: print is used to output one or more strings. It can only print out the value of simple type variables; the print_r() function can print out the value of complex type variables. Print out the array content and structure, and display the keys and elements in a certain format.

What is the difference between print_r and print

Analysis of the difference between print_r and print:

print()

print() To output one or more strings. Like echo, it is not actually a function. Print has a return value, but echo does not. When its execution fails, it returns false, and if it succeeds, it returns true. It is slightly slower than echo. It can only print out the values ​​of simple type variables, such as int and string.

For example: (write the value of string variable $str to the output)

<?php
    $str="hello world!";
    print $str;
?>

print_r() function

print_r() function can print Get the value of a complex type variable. Use print_r() to print out the entire array content and structure, and display the keys and elements in a certain format. In fact, it's not just for printing, but for printing easy-to-understand information about variables.

For example: (print array $age)

<?php
    $age=array(18,20,24);
    print_r($age);
?>
//运行结果:Array ( [0] => 18 [1] => 20 [2] => 24 )

If you want to learn more related knowledge, please visit php Chinese website.

The above is the detailed content of What is the difference between print_r and print. 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