Nullable return types in PHP7

In PHP7, nullable return types were introduced, allowing a function to return either a specific type or null. This is indicated by adding a question mark (?) before the type in the function declaration, for example "function example(): ?string {}" would indicate that the function "example" returns a string or null. This helps to prevent errors by making the code more explicit about what type of value the function can return.

Here's an example:

<?php


//To specify a nullable return type, you add a ? before the type
function divideNumbers(int $a, int $b): ?float
{
  if ($b === 0) {
    return null;
  }
  return $a / $b;
}

$result = divideNumbers(10, 2);

if ($result !== null) {
  echo "The result is: " . $result;
} else {
  echo "The division by zero is not allowed.";
}

?>

The above code will produce the following output:

The result is: 5