首页 > 后端开发 > php教程 > PHP 中的 Print_r()

PHP 中的 Print_r()

WBOY
发布: 2024-08-29 12:52:14
原创
1124 人浏览过

PHP 开发人员使用 print_r() 函数生成有关脚本中变量的人类可读信息。他们可以将函数的输出存储在变量中或直接在输出窗口上打印。存储或显示值的行为取决于提供给函数的输入参数。需要注意的是,从 PHP 版本 4 开始就支持 print_r(),包括 PHP4、PHP5 和 PHP7。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法:

您可以使用以下语法在任何 PHP 脚本中实现 print_r() 函数:

print_r ( mixed $variable_name , bool $returnType) : mixed
登录后复制

variable_name 参数的值是必需的,而 $returnType 是可选的。

PHP中print_r()的参数

该方法可接受两个参数作为输入参数。

1. $变量_名称

该参数是 print_r() 方法的强制输入参数。它指定函数需要以人类可读的格式提取信息的变量。对于类变量,此方法还捕获类成员的属性。

2. $returnType

此函数有一个可选的输入参数,允许您决定是否存储或打印输出。您可以使用此参数来确定所需的行为。

这是一个布尔类型的参数。该参数的默认值设置为FALSE。

Value of ReturnType Description
TRUE The function provides a return value that you can store in a variable.
FALSE The function prints the output; you cannot capture or store the value.
ReturnType 的值 描述 TRUE 该函数提供一个返回值,您可以将其存储在变量中。 FALSE 函数打印输出;您无法捕获或存储该值。 表>

Return Value:

The return value of the function depends on the type of the variable and the value of the returnType as the input argument. If a given variable is of type string, integer, or float, the return value is the variable itself as it is. When you set the return type to FALSE, and the input variable is an array or object, the function will return the keys and elements as the output values.

When the returnType is set to TRUE, print_r() results in a storable outcome.

Note: By default, the value for returnType is FALSE. Thus the default functionality of the print_r() method is the print/display the information of the given variable. To configure the function to capture the output and store it in different variables, the developer needs to use the parameter returnType in its print_r() function call by setting the value to TRUE.

Examples to Implement of print_r() in PHP

Below are the examples of print_r() in PHP:

Example #1

The code snippet below illustrates the functionality of print_r() in displaying information about a string variable, an integer variable, and an array input. To achieve this, you must include only the input variable as a parameter in the function call. In this case, the default value of the returnType parameter is FALSE.

Code:

<?php
// PHP program to illustrate the print_r() function to exhibit printing functionality :
// Declaring a string variable
$input_str = "An information string";
// Declaring a  integer variable
$input_int= 101;
// Declaring an array variable
$input_arr = array("Count1"=>"35", "Count2"=>"45", "Count3"=>"55");
// printing the variables
print_r("Printing the string variable information");
echo"\n";
print_r($input_str);
echo"\n";
echo"\n";
print_r("Printing the integer variable information");
echo"\n";
print_r($input_int);
echo"\n";
echo"\n";
print_r("Printing the array variable information");
echo"\n";
print_r($input_arr);
?>
登录后复制

Output:

PHP 中的 Print_r()

As mentioned earlier, when handling string and integer variables, the information is printed as it is. However, when it comes to an array variable, the output displays the data in the format of key-value pairs and their corresponding data types.

Example #2

By default, the value is FALSE. To capture and store the output from print_r() in a variable, you need to set the returnType parameter as TRUE in the function call.

Code:

<?php
// PHP program to illustrate the print_r() function exhibiting the storing functionality
// Declaring a string variable
$input_str = "An information string";
// Declaring an integer variable
$input_int= 5040;
// Declaring an array variable
$input_arr = array("element1"=>"31", "element2"=>"41", "element3"=>"51");
// Capturing and storing the output in different variables
print_r("Capturing the integer variable information");
echo"\n";
//Storing the integer variable output
$input_int_cap=print_r($input_int,true);
print_r($input_int_cap);
echo"\n";
echo"\n";
print_r("Capturing the string variable information");
echo"\n";
//Storing the string variable output
$input_str_cap=print_r($input_str,true);
print_r($input_str_cap);
echo"\n";
echo"\n";
print_r("Capturing the array variable information");
echo"\n";
//Storing the array variable output
$input_arr_cap=print_r($input_arr,true);
print_r($input_arr_cap);
?>
登录后复制

Output:

PHP 中的 Print_r()

Variables of their respective data types capture and store the information from integer and string variables. On the other hand, when dealing with an array variable, the function stores the output in a dedicated variable specifically designed to hold arrays. This storage encompasses the key-value pairs and the associated data types of each element.

The display result refers to the output produced using print_r() with storing variables.

Additional Note

  • This method can also show properties of private and protected properties of an object, whereas it has restricted features that will not show results for static class members.
  • In PHP 5, you can utilize the Reflection class to employ print_r() for static class member variables.
  • Developers commonly use print_r() for debugging PHP scripts.
  • Using the returnType parameter in the function call of print_r() involves the utilization of internal output buffering. As a result, it is impossible to employ print_r() within an ob_start() callback method.

Recommended Article

This is a guide to the print_r() in PHP. Here we discuss the Parameters of print_r() in PHP and its examples, along with Code Implementation. You can also go through our other suggested articles to learn more-

  1. PHP Frameworks
  2. PHP Array Search
  3. Polymorphism in PHP
  4. PHP GET Method

以上是PHP 中的 Print_r()的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
php
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板