What is the use of return in PHP?

Source: Internet
Author: User
Tags php class
Little brother just learned PHP a few days not quite understand return is not used to carry back a value in the function of the time used to use it.

See someone in the video today. echo "XXX" in a If statement followed by return
Return is not followed by anything ...
Why not break it here .....
What is the return used in more cases?
Please explain in detail, please, my little brother just get into the function of the big pile also see not very understand please speak of easy to understand some ...

function generally with return, do not write can, write, write a return 1 can also, in fact, there is a little area

As an example,

Saierfunction A () {     $b =1;    return $b;} Function B () {    $b =1;    echo $b;} echo a ();  output 1b ();   Output 1//appear to be output 1, and B () seems to be more convenient, in fact, if you want to use the return value of the function, B () will not be echo a () +1;  Output 2//b (); This will not be able to achieve this function, need to go inside the function to change, extension inconvenient

Never understand the use of return when should be used when should not use who can tell? Why add return to the following example?

<?php class man{    private $money =1000;    Public function Show () {      return $this->money*0.8;   }} $a =new man (); Echo $a->show ();? >

Return returns a result to the caller of the function/method.

For instance, your example calls the show () method to return an integer (800). The call is the equivalent of performing echo 800.

If your function/method is to perform some operation instead of returning data, you can return a Boolean value to indicate whether the operation succeeded or failed for process control where it was called.

Return what, your function is running out of what, if not return your function is empty after running
For example, function A () {return 1;}
Execute a () to get 1
$b = A (); Then $b is equal to 1, if not return then execute a () there is no return value, that is, empty, that $b is empty

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.