Π’ΠΎΠ·Π²Ρ€Π°Ρ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ

ЗначСния Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°ΡŽΡ‚ΡΡ Π½Π΅ΠΎΠ±ΡΠ·Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΠΉ инструкциСй Π²ΠΎΠ·Π²Ρ€Π°Ρ‚Π°. Π Π°Π·Ρ€Π΅ΡˆΠ°Π΅Ρ‚ΡΡ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Ρ‚ΡŒ любой Ρ‚ΠΈΠΏ, Π²ΠΊΠ»ΡŽΡ‡Π°Ρ массивы ΠΈ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Ρ‹. Π’ΠΎΠ·Π²Ρ€Π°Ρ‚ Π½Π΅ΠΌΠ΅Π΄Π»Π΅Π½Π½ΠΎ Π·Π°Π²Π΅Ρ€ΡˆΠ°Π΅Ρ‚ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ ΠΈ ΠΏΠ΅Ρ€Π΅Π΄Π°Ρ‘Ρ‚ ΡƒΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠ΅ ΠΎΠ±Ρ€Π°Ρ‚Π½ΠΎ β€” ΠΊ Ρ‚ΠΎΠΉ строкС ΠΊΠΎΠ΄Π°, Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ Π²Ρ‹Π·Π²Π°Π»ΠΈ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ. ΠŸΠΎΠ΄Ρ€ΠΎΠ±Π½ΡƒΡŽ ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΡŽ ΠΎ Π²ΠΎΠ·Π²Ρ€Π°Ρ‚Π΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ Π΄Π°Ρ‘Ρ‚ описаниС инструкции return.

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅:

Ѐункция Π²Π΅Ρ€Π½Ρ‘Ρ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ null, Ссли ΠΈΠ½ΡΡ‚Ρ€ΡƒΠΊΡ†ΠΈΡŽ return Π½Π΅ ΡƒΠΊΠ°Π·Π°Π»ΠΈ.

Π˜Π½ΡΡ‚Ρ€ΡƒΠΊΡ†ΠΈΡ return

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ с инструкциСй return

<?php

function square($num)
{
return
$num * $num;
}

echo
square(4); // Π’Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ 16

Ѐункция Π½Π΅ ΡƒΠΌΠ΅Π΅Ρ‚ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Ρ‚ΡŒ большС ΠΎΠ΄Π½ΠΎΠ³ΠΎ значСния, Π½ΠΎ Π°Π½Π°Π»ΠΎΠ³ΠΈΡ‡Π½ΠΎΠ³ΠΎ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Π° Π΄ΠΎΠ±ΠΈΠ²Π°ΡŽΡ‚ΡΡ Π²ΠΎΠ·Π²Ρ€Π°Ρ‚ΠΎΠΌ массива.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #2 Π’ΠΎΠ·Π²Ρ€Π°Ρ‚ Π½Π°Π±ΠΎΡ€Π° Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ Π² Π²ΠΈΠ΄Π΅ массива

<?php

function small_numbers()
{
return [
0, 1, 2];
}

// ДСструктуризация массива Π±ΡƒΠ΄Π΅Ρ‚ ΡΠΎΠ±ΠΈΡ€Π°Ρ‚ΡŒ ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ элСмСнт массива ΠΈΠ½Π΄ΠΈΠ²ΠΈΠ΄ΡƒΠ°Π»ΡŒΠ½ΠΎ
[$zero, $one, $two] = small_numbers();
var_dump($zero, $one, $two);

// Π”ΠΎ вСрсии 7.1.0 СдинствСнной эквивалСнтной Π°Π»ΡŒΡ‚Π΅Ρ€Π½Π°Ρ‚ΠΈΠ²ΠΎΠΉ Π±Ρ‹Π»Π° языковая конструкция list()
list($zero, $one, $two) = small_numbers();

ΠžΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ & ΡƒΠΊΠ°Π·Ρ‹Π²Π°ΡŽΡ‚ ΠΈ Π² описании Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ, ΠΈ Π² ΠΌΠΎΠΌΠ΅Π½Ρ‚ присваивания Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΠΎΠ³ΠΎ значСния ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ функция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π»Π° Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ ΠΏΠΎ ссылкС:

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #3 Π’ΠΎΠ·Π²Ρ€Π°Ρ‚ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Π° ΠΏΠΎ ссылкС

<?php

function &returns_reference()
{
return
$someref;
}

$newref =& returns_reference();

Π”ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½ΡƒΡŽ ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΡŽ ΠΎ ссылках Π΄Π°Ρ‘Ρ‚ Ρ€Π°Π·Π΄Π΅Π» Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚Π°Ρ†ΠΈΠΈ «ОбъяснСниС ссылок».

οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 4 notes

up
22
rstaveley at seseit dot com ΒΆ
15 years ago
Developers with a C background may expect pass by reference semantics for arrays. It may be surprising that  pass by value is used for arrays just like scalars. Objects are implicitly passed by reference.

<?php

# (1) Objects are always passed by reference and returned by reference

class Obj {
    public $x;
}

function obj_inc_x($obj) {
    $obj->x++;
    return $obj;
}

$obj = new Obj();
$obj->x = 1;

$obj2 = obj_inc_x($obj);
obj_inc_x($obj2);

print $obj->x . ', ' . $obj2->x . "\n";

# (2) Scalars are not passed by reference or returned as such

function scalar_inc_x($x) {
    $x++;
    return $x;
}

$x = 1;

$x2 = scalar_inc_x($x);
scalar_inc_x($x2);

print $x . ', ' . $x2 . "\n";

# (3) You have to force pass by reference and return by reference on scalars

function &scalar_ref_inc_x(&$x) {
    $x++;
    return $x;
}

$x = 1;

$x2 =& scalar_ref_inc_x($x);    # Need reference here as well as the function sig
scalar_ref_inc_x($x2);

print $x . ', ' . $x2 . "\n";

# (4) Arrays use pass by value sematics just like scalars

function array_inc_x($array) {
    $array{'x'}++;
    return $array;
}

$array = array();
$array['x'] = 1;

$array2 = array_inc_x($array);
array_inc_x($array2);

print $array['x'] . ', ' . $array2['x'] . "\n";

# (5) You have to force pass by reference and return by reference on arrays

function &array_ref_inc_x(&$array) {
    $array{'x'}++;
    return $array;
}

$array = array();
$array['x'] = 1;

$array2 =& array_ref_inc_x($array); # Need reference here as well as the function sig
array_ref_inc_x($array2);

print $array['x'] . ', ' . $array2['x'] . "\n";
up
17
ryan dot jentzsch at gmail dot com ΒΆ
9 years ago
PHP 7.1 allows for void and null return types by preceding the type declaration with a ? -- (e.g. function canReturnNullorString(): ?string) 

However resource is not allowed as a return type:

<?php
function fileOpen(string $fileName, string $mode): resource
{
    $handle = fopen($fileName, $mode);
    if ($handle !== false)
    {
        return $handle;
    }
}

$resourceHandle = fileOpen("myfile.txt", "r");
?>

Errors with:
Fatal error: Uncaught TypeError: Return value of fileOpen() must be an instance of resource, resource returned.
up
9
nick at itomic.com ΒΆ
22 years ago
Functions which return references, may return a NULL value. This is inconsistent with the fact that function parameters passed by reference can't be passed as NULL (or in fact anything which isnt a variable).

i.e.

<?php

function &testRet()
{
    return NULL;
}

if (testRet() === NULL)
{
    echo "NULL";
}
?>

parses fine and echoes NULL
up
9
bgalloway at citycarshare dot org ΒΆ
18 years ago
Be careful about using "do this thing or die()" logic in your return lines.  It doesn't work as you'd expect:

<?php
function myfunc1() {
    return('thingy' or die('otherthingy'));
}
function myfunc2() {
    return 'thingy' or die('otherthingy');
}
function myfunc3() {
    return('thingy') or die('otherthingy');
}
function myfunc4() {
    return 'thingy' or 'otherthingy';
}
function myfunc5() {
    $x = 'thingy' or 'otherthingy'; return $x;
}
echo myfunc1(). "\n". myfunc2(). "\n". myfunc3(). "\n". myfunc4(). "\n". myfunc5(). "\n";
?>

Only myfunc5() returns 'thingy' - the rest return 1.