Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Keyword return

End the current function and provide its result to the caller.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

return $result;

End the current function and provide its result to the caller. A bare return or ordinary completion gives null in an untyped function. In an included file, return hands control and an optional value back to the including script. Declared return types constrain the permitted forms.

function firstPositive(array $values) { foreach($values as $v){if($v>0){return $v;}} return; }
echo json_encode([firstPositive([0,4,8]),firstPositive([-1])]);
[4,null]

Examples

<?php
function firstPositive(array $values) { foreach($values as $v){if($v>0){return $v;}} return; }
echo json_encode([firstPositive([0,4,8]),firstPositive([-1])]);

Result

[4,null]

Sources