php中函数的类型提示和文件读取功能

这个没有深入。

<?php

    function addNumbers(int $a, int $b, bool $printSum): int {
        $sum = $a + $b;
        if ($printSum) {
            echo 'The sum is ' . $sum. '<br/>';
        }
        return $sum;
    }

    addNumbers(1, 2, true);
    echo "<br/>";
    addNumbers(10, 34, true);
    echo "<br/>";
    addNumbers(1, 46, true);

    $bookJson = file_get_contents('books.json');
    $books = json_decode($bookJson, true);
    foreach ($books as $key => $book) {
        foreach ($book as $bookKey => $bookItem) {
            echo $key . '. ' . $bookKey . '->' . $bookItem . '<br/>';
        }
        
    }
?>

books.json

[
    {
        "title": "To Kill A Mockingbird",
        "author": "Harper Lee",
        "available": true,
        "pages": 336,
        "isbn": 9780061120084
    },
    {
        "title": "1984",
        "author": "George Orwell",
        "available": true,
        "pages": 267,
        "isbn": 9780547249643
    },
    {
        "title": "One Hundred Years Of Solitude",
        "author": "Gabriel Garcia Marquez",
        "available": false,
        "pages": 457,
        "isbn": 9785267006323
    }
]

输出:

The sum is 3

The sum is 44

The sum is 47
0. title->To Kill A Mockingbird
0. author->Harper Lee
0. available->1
0. pages->336
0. isbn->9780061120084
1. title->1984
1. author->George Orwell
1. available->1
1. pages->267
1. isbn->9780547249643
2. title->One Hundred Years Of Solitude
2. author->Gabriel Garcia Marquez
2. available->
2. pages->457
2. isbn->9785267006323

猜你喜欢

转载自www.cnblogs.com/aguncn/p/11120519.html