Skip to content

Instantly share code, notes, and snippets.

@alfianmalik
Created May 24, 2020 16:20
Show Gist options
  • Save alfianmalik/2d2af593bc4dabd622765e7d942d2ace to your computer and use it in GitHub Desktop.
Save alfianmalik/2d2af593bc4dabd622765e7d942d2ace to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
/**
* @param bool $error
* @param null $message
* @return string
*/
function printError(bool $error, $message = null): ?string
{
if ($error) {
$message = 'You have encountered an error';
// Initial null variable now contains a string value
// even with strict type enforcement
}
return $message;
}
$message = printError(true, null);
echo $message; // 'You have encountered an error'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment