Delirando sobre informática, fotografía, programación, móviles y mucho más desde 2003
Aún después de años programando en PHP se descubren cosas nuevas. Hoy gracias a mi compañero Luis descubro algo que lleva implementado ¡¡¡desde PHP3!!! pero que nunca se me había ocurrido o no había necesitado: retornar valores con un include o require:
It is possible to execute a return() statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it’s possible to return values from included files. You can take the value of the include call as you would a normal function. This is not, however, possible when including remote files unless the output of the remote file has valid PHP start and end tags (as with any local file). You can declare the needed variables within those tags and they will be introduced at whichever point the file was included.
Es decir, que si creamos el archivo test.php:
<?php
function test() { }
return 'OK';
?>
Al hacer este include, la variable $res tendría el valor ‘OK’:
$res = include('test.php');
Una vez se conoce esta funcionalidad, puede sacársele partido ![]()
Delirios de un Informático :: Gestionado con WordPress :: Alojado en Bitacoras.com :: Licenciado bajo Creative Commons