How to return value from function in php

青灯夜游
Release: 2023-03-11 17:54:02
Original
4123 people have browsed it

In PHP, you can use the return statement to return a value from a function, which can return the running result of the function. The syntax format is "return return value;"; the return statement is used to return a value to the "function caller" , after returning the value, the function execution ends immediately, so the return statement is generally placed at the end of the function.

How to return value from function in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

PHP return: function Return value

PHP provides the return statement to return the running result of the function. Its syntax format is as follows:

return 返回值;
Copy after login

Among them, the "return value" is an optional parameter , can be a specific value or expression, or can be empty. The "return value" and the return keyword need to be separated by a space.

Note: The return statement can only return one parameter, that is, it can only return one value, and cannot return multiple values ​​at one time. If you want to return multiple values, you need to define an array in the function, store the return value in the array and return it.

You need to pay attention to the following points when using the return statement:

  • The return statement is used to return a value to the "caller of the function". After returning the value, immediately End the function running, so the return statement is generally placed at the end of the function;

  • If there are multiple return statements in a function, only the first one will be executed;

  • The return statement can also return no parameters, which is equivalent to ending the function running;

  • If the return statement is used in the global scope, the currently running function will be terminated immediately. Script;

  • If the script file introduced using include or require contains a return statement, it will return to the place where the script was introduced and continue to execute downwards, and other codes after return will not be executed.

[Example] The following implements a function that calculates the exchange rate, and returns the calculated value through return. The code is as follows:

<?php
    function money($rmb, $rate=0.1438){
        $price = $rmb * $rate;
        return $price;
    }
    $dollar = money(50);
    echo &#39;50 元人民币 = &#39;.$dollar.&#39; 美元&#39;;
?>
Copy after login

The running results are as follows:

50 元人民币 = 7.19 美元
Copy after login

Tip: In the above example, we used return in the function to return a variable $price. In fact, what is returned is not the variable itself, but the value of this variable. So outside the function we need to use another variable to store this value.

return can not only return a variable, but also an expression, so we can write the function in the above example more concisely, as shown below:

<?php
    function money($rmb, $rate=0.1438){
        return $rmb * $rate;
    }
?>
Copy after login

Recommended learning: "PHP video tutorial

The above is the detailed content of How to return value from function in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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