2022年3月30日水曜日
2022年3月28日月曜日
PHP foreach
<?php
$scores = [
'first' => 90,
'second' => 40,
'third' => 100,
];
// foreach ($scores as $value) {
// foreach ($scores as $score) {
// echo $score . PHP_EOL;
// }
foreach ($scores as $key => $score) {
echo $key . ' - ' . $score . PHP_EOL;
}
2022年3月26日土曜日
2022年3月24日木曜日
2022年3月21日月曜日
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;
2022年3月18日金曜日
PHP 引数の型
<?php
declare(strict_types=1);
function showInfo(string $name, int $score): void
{
echo $name . ': ' . $score . PHP_EOL;
}
// showInfo('taguchi', 40);
// showInfo('taguchi', 'dotinstall');
showInfo('taguchi', '4');