How to use a function in php to return a variable

醉折花枝作酒筹
Release: 2023-03-11 22:20:01
Original
1955 people have browsed it

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);
?>
Copy after login

The result is

How to use a function in php to return a variable

#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 ] )
Copy after login

$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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!