Value Return Constructions Return Construction

Lecture



The rerurn construction returns values, mostly from user-defined functions, as parameters of a functional query. When return is called, the execution of the user-defined function is interrupted, and the return construct returns certain values.

If the return construct is called from the global definition area (outside of user-defined functions), the script will also complete its work, and return will also return specific values.

Preferably, the return construct is used to return values ​​by user-defined functions.

Return values ​​can be of any type, including lists and objects. The return results in the completion of the function and the transfer of control back to the line of code in which the function was called.

An example of using the return construct to return integer values:

<?php
function retfunct()
{
return 7;
}
echo retfunct(); // выводит '7'.
?>

An example of a return with a return array construct:

<? php
function numbers ()
{
      return array ( 0 ,   1 ,   2 );
}
list ($ zero ,   $ one ,   $ two )   = numbers ();
echo $ zero ;
echo $ one ;
echo $ two ;
// Выводит '012'
?>

In order for a function to return a result by reference, you need to use the & operator both when describing a function, and when assigning a return value to a variable:

<?php
function &returns_reference()
{
return $someref;
}

$newref =& returns_reference();
?>

As we can see, the return construct is very convenient for use in user-defined functions.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Running server side scripts using PHP as an example (LAMP)

Terms: Running server side scripts using PHP as an example (LAMP)