PHP null

 ~ $ php main.php

Gold Medal

Fatal error: Uncaught TypeError: Return value of getAward() must be of the type string, null returned in /home/dotinstall/main.php:7

Stack trace:

#0 /home/dotinstall/main.php(11): getAward(40)

#1 {main}

  thrown in /home/dotinstall/main.php on line 7

~ $ php main.php

Gold Medal

~ $ php main.php

Gold Medal

~ $ 

<?php

declare(strict_types=1);

function getAward(?int $score): ?string

{

  return $score >= 100 ? ‘Gold Medal’ : null;

}

echo getAward(150) .PHP_EOL;

echo getAward(40) .PHP_EOL;

PHP 引数

 <?php

function showAd($message = ‘Ad’) //仮引数

{

  echo ‘———-‘ . PHP_EOL;

  echo ‘— ‘ . $message . ‘ —‘ . PHP_EOL;

  echo ‘———-‘ . PHP_EOL;

}

showAd(‘Header Ad’); //実引数

echo ‘Tom is great!’ . PHP_EOL;

echo ‘Bob is great!’ . PHP_EOL;

// showAd(‘Ad’);

showAd();

echo ‘Steve is great!’ . PHP_EOL;

echo ‘Bob is great!’ . PHP_EOL;

showAd(‘Footer Ad’);