回顾:收据文本
Coddy 面向 Ruby 开发者的 PHP 课程的第 7 课,共 48 课。
挑战
简单输入为 [name, quantity, unitCents]。整数的取值范围为 0 到 1000(含边界)。receipt 返回 NAME: TOTAL,冒号后有一个空格。
提供的 main 脚本会从标准输入读取一个 JSON 值,检查指定的公共 API,并打印结果。实现模块声明时不要打印内容;不要替换启动脚本,也不要从模块读取输入。输入符合指定的类型和取值范围。
编辑 Functions.php。main.php 是锁定的入口文件。依赖项使用带相对文件名的 require_once __DIR__。
亲手试试
<?php
declare(strict_types=1);
require_once __DIR__."/Functions.php";
function ensure(bool $ok,string $message):void{if(!$ok)throw new RuntimeException("API: ".$message);}
$input=json_decode(trim(stream_get_contents(STDIN)),true,512,JSON_THROW_ON_ERROR);
ensure(receipt("x",4,7)==="x: 28","receipt");
echo receipt(...$input),"\n";
面向 Ruby 开发者的 PHP中的所有课
自己练习: PHP 在线编译器