PHP | print_r() Function (original) (raw)
Last Updated : 26 Apr, 2018
The print_r() function is a built-in function in PHP and is used to print or display information stored in a variable.
Syntax:
print_r( variable,variable, variable,isStore )
Parameters: This function accepts two parameters as shown in above syntax and described below.
- $variable: This parameter specifies the variable to be printed and is a mandatory parameter.
- $isStore: This an option parameter. This parameter is of boolean type whose default value is FALSE and is used to store the output of the print_r() function in a variable rather than printing it. If this parameter is set to TRUE then the print_r() function will return the output which it is supposed to print.
Return Value: If the variableisanintegerorafloatorastringthefunctionprintsthevalueofthevariable.Ifthevariableisanarraythefunctionprintsthearrayinaformatwhichdisplaysthekeysaswellasvalues,asimilarnotationisusedforobjects.Iftheparametervariable is an integer or a float or a string the function prints the value of the variable. If the variable is an array the function prints the array in a format which displays the keys as well as values, a similar notation is used for objects. If the parameter variableisanintegerorafloatorastringthefunctionprintsthevalueofthevariable.Ifthevariableisanarraythefunctionprintsthearrayinaformatwhichdisplaysthekeysaswellasvalues,asimilarnotationisusedforobjects.IftheparameterisStore is set to TRUE then the print_r() function will return a string containing the information which it is supposed to print.
Below programs illustrate the print_r() function:
Program 1:
<?php
$var1
=
"Welcome to GeeksforGeeks"
;
$var2
= 101;
$arr
=
array
(
'0'
=>
"Welcome"
,
'1'
=>
"to"
,
'2'
=>
"GeeksforGeeks"
);
print_r(
$var1
);
echo
"\n"
;
print_r(
$var2
);
echo
"\n"
;
print_r(
$arr
);
?>
Output:
Welcome to GeeksforGeeks 101 Array ( [0] => Welcome [1] => to [2] => GeeksforGeeks )
Program 2:
<?php
$arr
=
array
(
'0'
=>
"Welcome"
,
'1'
=>
"to"
,
`` '2'
=>
"GeeksforGeeks"
);
$results
= print_r(
$arr
, true);
echo
$results
;
?>
Output:
Array ( [0] => Welcome [1] => to [2] => GeeksforGeeks )