Skip to content

Instantly share code, notes, and snippets.

@yoander
Last active July 14, 2018 22:19
Show Gist options
  • Save yoander/7678930d742956789d3fc5cb1ce123e7 to your computer and use it in GitHub Desktop.
Save yoander/7678930d742956789d3fc5cb1ce123e7 to your computer and use it in GitHub Desktop.
PHP 7.1 New Features
<?php
function returnStringOrNull($valueToReturn): ?string {
return $valueToReturn;
}
echo 'LibreByte:', gettype(returnStringOrNull('LibreByte')), nl2br("\n");
echo 'null:', gettype(returnStringOrNull(null)), nl2br("\n");
echo '25:', gettype(returnStringOrNull(25)), nl2br("\n");
echo 'SdtClass object:', gettype(returnStringOrNull(new StdClass())), nl2br("\n");
<?php
function stringOrNullParam(?string $param) {
echo 'Param: ', is_null($param) ? "'null'" : "'$param'", ", type: ", gettype($param), nl2br("\n");
}
stringOrNullParam('LibreByte');
stringOrNullParam(null);
stringOrNullParam(25);
stringOrNullParam(new StdClass());
<?php
a = 10;
function voidWithoutReturn(&$param, $newValue): void {
$param = $newValue;
}
function voidWithEmptyReturn(&$param, $newValue): void {
$param = $newValue;
return;
}
echo 'Initial value for a: ', $a, nl2br("\n");
voidWithoutReturn($a, 25);
echo 'Called voidWithoutReturn($a, 25), $a = ', $a, nl2br("\n");
voidWithEmptyReturn($a, 30);
echo 'Called voidWithEmptyReturn($a, 30), a = ', $a, nl2br("\n");
<?php
function voidWithReturn(): void {
return 1;
}
<?php
function voidWithReturnNull(): void {
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment