CKTIS / PHP
Palabra clave return
Termina la función y entrega el resultado al llamante.
Versión: PHP 8.4; repository runtime, production runtime not attested
Sintaxis
return $result;Termina la función y entrega el resultado al llamante. En una función sin tipo de retorno un return vacío o llegar al final da null. En un archivo incluido devuelve el control y un valor opcional al script que lo incluyó. El tipo de retorno restringe las formas válidas.
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]
Ejemplos
<?php
function firstPositive(array $values) { foreach($values as $v){if($v>0){return $v;}} return; }
echo json_encode([firstPositive([0,4,8]),firstPositive([-1])]);
Resultado
[4,null]