Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * "?string" indica que a função pode retornar um valor "null" ou uma "string"
- */
- function retorno($var): ?string
- {
- return $var;
- }
- /**
- * Caso remova "?" de function retorno($var): string o erro abaixo será gerado
- * Uncaught TypeError: Return value of retorno() must be of the type string, null returned in
- */
- echo retorno(null);
- /**
- * Aqui será retornado "Boi" como uma string
- */
- echo retorno("Boi");
- /**
- * Sim, uma "string" (que não esteja vazia) é diferente de um valor "null"
- */
- var_dump("Boi" <> null);
Advertisement
Add Comment
Please, Sign In to add comment