Home Backend Development PHP Problem How many return values ​​can a php function have?

How many return values ​​can a php function have?

Apr 26, 2022 pm 08:14 PM
php php function

php function can only have one return value. In PHP, the function return value is defined using the return statement, with the syntax "return return value;". The return statement can only return one parameter, that is, the function can only have one return value; 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.

How many return values ​​can a php function have?

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

The return value of the php function can only be one.

A function is a collection of functions that can implement certain specific functions or operations. It makes no sense to keep the result after the function is run inside the function, so we need to return the result of the function to the place where the function is called.

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

return 返回值;

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

<?php
function square($num){
    return $num * $num;
}
echo square(4);  //outputs&#39;16&#39;.
?>

The running result of the above code is:

16

Note: The function can only have one return value.

#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;;
?>

The running results are as follows:

50 元人民币 = 7.19 美元

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.

Through the above introduction, we know that return can only return one parameter at a time. If you want to return multiple parameters, you need to use an array to complete it. Let's demonstrate it through an example:

<?php
function small_numbers(){
    return array(0, 1, 2);
}
list($zero, $one, $two) = small_numbers();
echo $zero . $one . $two;
?>

Code The execution result is:

012

$zero $one $two The values ​​are 0, 1, and 2 respectively.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How many return values ​​can a php function have?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1594
276
edge pdf viewer not working edge pdf viewer not working Aug 07, 2025 pm 04:36 PM

TestthePDFinanotherapptodetermineiftheissueiswiththefileorEdge.2.Enablethebuilt-inPDFviewerbyturningoff"AlwaysopenPDFfilesexternally"and"DownloadPDFfiles"inEdgesettings.3.Clearbrowsingdataincludingcookiesandcachedfilestoresolveren

Yii Developer: Mastering the Essential Technical Skills Yii Developer: Mastering the Essential Technical Skills Aug 04, 2025 pm 04:54 PM

To become a master of Yii, you need to master the following skills: 1) Understand Yii's MVC architecture, 2) Proficient in using ActiveRecordORM, 3) Effectively utilize Gii code generation tools, 4) Master Yii's verification rules, 5) Optimize database query performance, 6) Continuously pay attention to Yii ecosystem and community resources. Through the learning and practice of these skills, the development capabilities under the Yii framework can be comprehensively improved.

VS Code shortcut to focus on explorer panel VS Code shortcut to focus on explorer panel Aug 08, 2025 am 04:00 AM

In VSCode, you can quickly switch the panel and editing area through shortcut keys. To jump to the left Explorer panel, use Ctrl Shift E (Windows/Linux) or Cmd Shift E (Mac); return to the editing area to use Ctrl ` or Esc or Ctrl 1~9. Compared to mouse operation, keyboard shortcuts are more efficient and do not interrupt the encoding rhythm. Other tips include: Ctrl KCtrl E Focus Search Box, F2 Rename File, Delete File, Enter Open File, Arrow Key Expand/Collapse Folder.

go by example running a subprocess go by example running a subprocess Aug 06, 2025 am 09:05 AM

Run the child process using the os/exec package, create the command through exec.Command but not execute it immediately; 2. Run the command with .Output() and catch stdout. If the exit code is non-zero, return exec.ExitError; 3. Use .Start() to start the process without blocking, combine with .StdoutPipe() to stream output in real time; 4. Enter data into the process through .StdinPipe(), and after writing, you need to close the pipeline and call .Wait() to wait for the end; 5. Exec.ExitError must be processed to get the exit code and stderr of the failed command to avoid zombie processes.

Fixed: Windows Update Failed to Install Fixed: Windows Update Failed to Install Aug 08, 2025 pm 04:16 PM

RuntheWindowsUpdateTroubleshooterviaSettings>Update&Security>Troubleshoottoautomaticallyfixcommonissues.2.ResetWindowsUpdatecomponentsbystoppingrelatedservices,renamingtheSoftwareDistributionandCatroot2folders,thenrestartingtheservicestocle

How to work with arrays in php How to work with arrays in php Aug 20, 2025 pm 07:01 PM

PHParrayshandledatacollectionsefficientlyusingindexedorassociativestructures;theyarecreatedwitharray()or[],accessedviakeys,modifiedbyassignment,iteratedwithforeach,andmanipulatedusingfunctionslikecount(),in_array(),array_key_exists(),array_push(),arr

Fix: Ethernet 'Unidentified Network' Fix: Ethernet 'Unidentified Network' Aug 12, 2025 pm 01:53 PM

Restartyourrouterandcomputertoresolvetemporaryglitches.2.RuntheNetworkTroubleshooterviathesystemtraytoautomaticallyfixcommonissues.3.RenewtheIPaddressusingCommandPromptasadministratorbyrunningipconfig/release,ipconfig/renew,netshwinsockreset,andnetsh

Compare and contrast PHP Traits, Abstract Classes, and Interfaces with practical use cases. Compare and contrast PHP Traits, Abstract Classes, and Interfaces with practical use cases. Aug 11, 2025 pm 11:17 PM

Useinterfacestodefinecontractsforunrelatedclasses,ensuringtheyimplementspecificmethods;2.Useabstractclassestosharecommonlogicamongrelatedclasseswhileenforcinginheritance;3.Usetraitstoreuseutilitycodeacrossunrelatedclasseswithoutinheritance,promotingD

See all articles