I need to print contents of multiple arrays in my code. Eg
function performOp($n, $inputArr, $workArr)
{
printf("Entered function, value of n is %d", $n);
print_r($inputArr);
print_r($workArr);
$width =0;
}
Now, Instead of writing print_r twice, is there any way I can write a single statement and print both the arrays ?
Also, If I want to print “Input array value is ” before displaying the Array{}, is there a way to do so using printf or any other function?
I tried writing
printf("Value of inputArray is %s ", print_r($inputArr)
, but this would not work.
Any help is really appreciated.
Thanks
Dumping the Variables
You can pass multiple arrays into
var_dump().For instance, the following:
Outputs this:
Note how it keeps both arrays distinct in the output as well.
Function with n-arguments
You could also use a function, calling upon
func_get_args()for the arrays passed in:Which, in this case, would output the following:
Using
json_encode()instead ofprint_rwould output a slightly more readable format: