Home > Article > Backend Development > How to use a function in php to return a variable
In the previous article, we learned how to use functions to destroy variables. If necessary, please read "How to use functions to destroy variables in php". This time we will introduce to you the method of using a function to return a variable. You can refer to it if you need it.
In PHP, we often need to return or output variables, so what should we do at this time? Today we will introduce the method of returning or outputting variables.
First let’s look at a small example.
<?php $a = array ("one","two", array ("apple", "orange", "pear", array (1, 2))); var_export ($a); echo "<br>"; print_r($a); ?>
The result is
#You can see that the arrays we wrote have been output, and it does not use echo, print this This is familiar to us, but it is roughly the same as the print output, with just a few differences.
Then let’s study this function systematically.
var_export() function is used to output or return a variable, expressed in string form. This function returns structural information about the variable passed to the function. It is similar to var_dump(), except that it returns a valid PHP code. In other words, the code returned by var_export can be directly assigned to a variable as PHP code. And this variable will get the same type of value as var_export.
Let’s take a look at the syntax of this function.
mixed var_export (要输出的变量, bool $return ] )
$return is an optional parameter. If set to TRUE, the function will not execute the output result, but will return the output result to a variable. And the return value is only available when $return is set to true, and the structure information of the variable is returned.
var_export() will always retain the structured form to store data. But please remember in particular: the type of the variable value at this time is already a string ($var), and the value inside can no longer be taken out in the form of an array.
That’s all. If you want to know anything else, you can click this. → →php video tutorial
The above is the detailed content of How to use a function in php to return a variable. For more information, please follow other related articles on the PHP Chinese website!