WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML


 

PHP 7 - Return Type Declarations


In PHP 7, another component, Return type declarations has been presented. Return type announcement determines the types of specifies worth that a function should return. Following types s for return types can be declared.

  • int
  • float
  • bool
  • string
  • interfaces
  • array
  • callable

Example - Valid Return Type

<?php
   declare(strict_types = 1);
   function returnIntValue(int $value): int {
      return $value;
   }
   print(returnIntValue(5));
?>

It produces the following browser output −

5

Example - Invalid Return Type

<?php
   declare(strict_types = 1);
   function returnIntValue(int $value): int {
      return $value + 1.0;
   }
   print(returnIntValue(5));
?>

It produces the following browser output −

Fatal error: Uncaught TypeError: Return value of returnIntValue() must be of the type integer, float returned...