Print_r() in PHP

WBOY
Freigeben: 2024-08-29 12:52:14
Original
950 Leute haben es durchsucht

PHP-Entwickler verwenden die Funktion print_r(), um für Menschen lesbare Informationen über eine Variable in einem Skript zu generieren. Sie können die Ausgabe der Funktion in einer Variablen speichern oder sie direkt im Ausgabefenster drucken. Das Verhalten beim Speichern oder Anzeigen der Werte hängt von den Eingabeargumenten ab, die der Funktion bereitgestellt werden. Es ist wichtig zu beachten, dass print_r() seit PHP Version 4 unterstützt wird, einschließlich PHP4, PHP5 und PHP7.

Starten Sie Ihren kostenlosen Softwareentwicklungskurs

Webentwicklung, Programmiersprachen, Softwaretests und andere

Syntax:

Sie können die Funktion print_r() in jedem PHP-Skript mit der folgenden Syntax implementieren:

print_r ( mixed $variable_name , bool $returnType) : mixed
Nach dem Login kopieren

Der Wert des Parameters variable_name ist obligatorisch, während $returnType optional ist.

Parameter von print_r() in PHP

Zwei Parameter werden von der Methode als Eingabeargumente akzeptiert.

1. $variable_name

Dieser Parameter ist das obligatorische Eingabeargument für die print_r()-Methode. Es gibt die Variable an, über die die Funktion die Informationen in einem für Menschen lesbaren Format extrahieren muss. Im Falle einer Klassenvariablen erfasst diese Methode auch die Eigenschaften der Klassenmitglieder.

2. $returnType

Diese Funktion verfügt über ein optionales Eingabeargument, mit dem Sie entscheiden können, ob die Ausgabe gespeichert oder gedruckt werden soll. Mit diesem Parameter können Sie das gewünschte Verhalten bestimmen.

Dies ist ein boolescher Parameter. Der Standardwert dieses Parameters ist auf FALSE gesetzt.

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.
Wert von ReturnType Beschreibung WAHR Die Funktion liefert einen Rückgabewert, den Sie in einer Variablen speichern können. FALSE Die Funktion druckt die Ausgabe; Sie können den Wert nicht erfassen oder speichern.

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);
?>
Nach dem Login kopieren

Output:

Print_r() in PHP

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);
?>
Nach dem Login kopieren

Output:

Print_r() in PHP

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

Das obige ist der detaillierte Inhalt vonPrint_r() in PHP. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
php
Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!